#include "qdrawutil.h"
#include <QStyleOption>
#include <QPainter>
#include <QToolBar>
#include <QPushButton>
#include <QStyleOptionButton>
#include <QApplication>
#include "fluentStyle.h"
#include "styleprivate.h"
#include <QListView>
#include <QDebug>

#ifdef USE_THEME_COLOR
#ifdef Q_OS_WIN
#include <Windows.h>
#include <dwmapi.h>

#pragma comment(lib, "dwmapi.lib")
#endif
#endif

#ifdef DEBUG
#define DEBUG_OUT_PUT(s) qDebug() << s
#else
#define DEBUG_OUT_PUT(s)
#endif

static const int windowsItemFrame        =  2; // menu item frame width
static const int windowsItemHMargin      =  3; // menu item hor text margin
static const int windowsItemVMargin      =  0; // menu item ver text margin
static const int windowsArrowHMargin     =  6; // arrow horizontal margin
static const int windowsRightBorder      = 12; // right border on windows

static QWindow *qt_getWindow(const QWidget *widget)
{
    return widget ? widget->window()->windowHandle() : 0;
}

static QColor mergedColors(const QColor &colorA, const QColor &colorB, int factor = 50)
{
    const int maxFactor = 100;
    QColor tmp = colorA;
    tmp.setRed((tmp.red() * factor) / maxFactor + (colorB.red() * (maxFactor - factor)) / maxFactor);
    tmp.setGreen((tmp.green() * factor) / maxFactor + (colorB.green() * (maxFactor - factor)) / maxFactor);
    tmp.setBlue((tmp.blue() * factor) / maxFactor + (colorB.blue() * (maxFactor - factor)) / maxFactor);
    return tmp;
}

QPainterPath windowPanelPath(const QRectF &r)
{
    static const qreal CornerPointOffset = 5.5;
    static const qreal CornerControlOffset = 2.1;

    QPainterPath path;
    // Top-left corner
    path.moveTo(r.left(), r.top() + CornerPointOffset);
    path.cubicTo(r.left(), r.top() + CornerControlOffset,
                 r.left() + CornerControlOffset, r.top(),
                 r.left() + CornerPointOffset, r.top());
    // Top-right corner
    path.lineTo(r.right() - CornerPointOffset, r.top());
    path.cubicTo(r.right() - CornerControlOffset, r.top(),
                 r.right(), r.top() + CornerControlOffset,
                 r.right(), r.top() + CornerPointOffset);
    // Bottom-right corner
    path.lineTo(r.right(), r.bottom() - CornerPointOffset);
    path.cubicTo(r.right(), r.bottom() - CornerControlOffset,
                 r.right() - CornerControlOffset, r.bottom(),
                 r.right() - CornerPointOffset, r.bottom());
    // Bottom-right corner
    path.lineTo(r.left() + CornerPointOffset, r.bottom());
    path.cubicTo(r.left() + CornerControlOffset, r.bottom(),
                 r.left(), r.bottom() - CornerControlOffset,
                 r.left(), r.bottom() - CornerPointOffset);
    path.lineTo(r.left(), r.top() + CornerPointOffset);

    return path;
}

#ifdef USE_THEME_COLOR
#ifdef Q_OS_WIN
QColor getAccentColor() {
    DWORD color = 0;
    BOOL opaque = FALSE;
    if (SUCCEEDED(::DwmGetColorizationColor(&color, &opaque))) {
        // 颜色格式: 0xAARRGGBB，需要转换为Qt颜色格式
        BYTE R, G, B;
        R = (color >> 0x0F) % 0x100;
        G = (color >> 0x08) % 0x100;
        B = color % 0x100;
        return QColor(R, G, B);
    }
    return QColor(0, 120, 215); // 默认值 - Windows蓝色
}
#endif
#endif

CFluentStyle::CFluentStyle()
    : QProxyStyle()
    , d_ptr(new CMyTestStylePrivate())
{
    QPalette palette = QApplication::palette();
    palette.setColor(QPalette::Active, QPalette::Window, QColor(windowColor));
    palette.setColor(QPalette::Active, QPalette::WindowText, QColor(windowTextColor));
    palette.setColor(QPalette::Active, QPalette::Base, QColor(baseColor));
    palette.setColor(QPalette::Active, QPalette::AlternateBase, QColor(alternateBaseColor));
    palette.setColor(QPalette::Active, QPalette::ToolTipBase, QColor(toolTipBaseColor));
    palette.setColor(QPalette::Active, QPalette::ToolTipText, QColor(toolTipTextColor));
    palette.setColor(QPalette::Active, QPalette::PlaceholderText, QColor(placeholderTextColor));
    palette.setColor(QPalette::Active, QPalette::Text, QColor(buttonTextColor));
    palette.setColor(QPalette::Active, QPalette::Button, QColor(buttonColor));
    palette.setColor(QPalette::Active, QPalette::ButtonText, QColor(buttonTextColor));
    palette.setColor(QPalette::Active, QPalette::Highlight, QColor(highlightColor));

#ifdef USE_THEME_COLOR
#ifdef Q_OS_WIN
    palette.setColor(QPalette::Active, QPalette::Highlight, getAccentColor());
    palette.setColor(QPalette::Disabled, QPalette::Highlight, getAccentColor());
#endif
#endif

    QApplication::setPalette(palette);
}

CFluentStyle::~CFluentStyle()
{
    if (d_ptr)
    {
        delete d_ptr;
    }
}

void drawArrow(const QStyleOption *opt, QPainter *painter, Qt::ArrowType arrowType)
{
    bool bEnable = opt->state & QStyle::State_Enabled;
    bool bMouseOver = opt->state & QStyle::State_MouseOver;
    QColor arrowColor;
    if (!bEnable) {
        arrowColor = opt->palette.color(QPalette::Disabled, QPalette::ButtonText);
    } else if (bMouseOver) {
        arrowColor = opt->palette.color(QPalette::Active, QPalette::Highlight);
    } else {
        arrowColor = opt->palette.color(QPalette::Active, QPalette::ButtonText);
    }
    painter->save();
    painter->setRenderHint(QPainter::Antialiasing, true);
    painter->setPen(QPen(arrowColor, 1.5, Qt::SolidLine, Qt::RoundCap));

    int arrowSize = 6;
    int centerX = opt->rect.center().x();
    int centerY = opt->rect.center().y();

    switch (arrowType) {
    case Qt::UpArrow:
    {
        // 上箭头
        painter->drawLine(centerX - arrowSize/2, centerY + arrowSize/3,
                        centerX, centerY - arrowSize/3);
        painter->drawLine(centerX, centerY - arrowSize/3,
                        centerX + arrowSize/2, centerY + arrowSize/3);
    }
        break;
    case Qt::DownArrow:
    {
        // 下箭头
        painter->drawLine(centerX - arrowSize/2, centerY - arrowSize/3,
                        centerX, centerY + arrowSize/3);
        painter->drawLine(centerX, centerY + arrowSize/3,
                        centerX + arrowSize/2, centerY - arrowSize/3);
    }
        break;
    case Qt::LeftArrow:
    {
        //左箭头
        painter->drawLine(centerX + arrowSize/3, centerY - arrowSize/2,
                        centerX - arrowSize/3, centerY);
        painter->drawLine(centerX + arrowSize/3, centerY + arrowSize/2,
                        centerX - arrowSize/3, centerY);
    }
        break;
    case Qt::RightArrow:
    {
        //右箭头
        painter->drawLine(centerX - arrowSize/3, centerY - arrowSize/2,
                        centerX + arrowSize/3, centerY);
        painter->drawLine(centerX - arrowSize/3, centerY + arrowSize/2,
                        centerX + arrowSize/3, centerY);
    }
        break;
    default:
        break;
    }
    painter->restore();
}

void CFluentStyle::drawPrimitive(QStyle::PrimitiveElement pe, const QStyleOption *opt, QPainter *painter, const QWidget *w) const
{
//    QProxyStyle::drawPrimitive(pe, opt, p, w);

    QRect rect = opt->rect;
    int state = opt->state;

#if 1
    switch (pe)
    {
    case PE_Frame:
    {
        bool bMouseOver = opt->state & QStyle::State_MouseOver;
        bool bFocus = opt->state & QStyle::State_HasFocus;

        QColor frameColor;
        if (bMouseOver || bFocus)
        {
            frameColor = opt->palette.color(QPalette::Active, QPalette::Highlight);
        }
        else
        {
            frameColor = opt->palette.color(QPalette::Inactive, QPalette::ButtonText);
        }
        painter->save();
        painter->setRenderHint(QPainter::Antialiasing, true);
        painter->setPen(frameColor);
        painter->setBrush(Qt::NoBrush);
        painter->drawRoundedRect(QRectF(opt->rect).adjusted(0.5, 0.5, -0.5, -0.5), 4, 4);
        painter->restore();
        break;
    }
    case PE_FrameDefaultButton:
    {
        DrawDefaultButton(opt, painter);
        break;
    }
    case PE_FrameDockWidget:
        if (const QStyleOptionFrame *frame = qstyleoption_cast<const QStyleOptionFrame *>(opt)) {
            int lw = frame->lineWidth;
            if (lw <= 0)
                lw = proxy()->pixelMetric(PM_DockWidgetFrameWidth, opt);

            //qDrawShadePanel(p, frame->rect, frame->palette, false, lw);
            painter->setPen(QColor(Qt::blue));
            painter->drawRoundedRect(opt->rect.adjusted(10, 10, -10, -10), 4, 4);
        }
        DEBUG_OUT_PUT("PE_FrameDockWidget");
        break;
    case PE_FrameFocusRect:
//        if (const QStyleOptionFocusRect *fropt = qstyleoption_cast<const QStyleOptionFocusRect *>(opt)) {
//            if (opt->state & QStyle::State_HasFocus)
//            {
//                QColor bg = fropt->backgroundColor;
//                QPen oldPen = p->pen();
//                if (bg.isValid()) {
//                    int h, s, v;
//                    bg.getHsv(&h, &s, &v);
//                    if (v >= 128)
//                        p->setPen(Qt::black);
//                    else
//                        p->setPen(Qt::white);
//                } else {
//                    p->setPen(opt->palette.windowText().color());
//                }
//    //            QRect focusRect = subElementRect(QStyle::SE_PushButtonContents, opt, w);
//                QRect focusRect = opt->rect.adjusted(-3, -3, -1, -1);
//                p->drawRect(focusRect.adjusted(0, 0, 3, 3)); //draw pen inclusive
//                p->setPen(oldPen);
//            }
//        }
//        qDebug() << "PE_FrameFocusRect";
        break;
    case PE_FrameGroupBox:
        if (const QStyleOptionFrame *frame = qstyleoption_cast<const QStyleOptionFrame *>(opt))
        {
            painter->save();
            painter->setRenderHint(QPainter::Antialiasing);
            if (frame->features & QStyleOptionFrame::Flat) {
                QRect fr = frame->rect;
                QPoint p1(fr.x(), fr.y() + 1);
                QPoint p2(fr.x() + fr.width(), p1.y());
                painter->drawLine(p1, p2);
            } else {
//                qDrawShadeRect(p, frame->rect.x(), frame->rect.y(), frame->rect.width(),
//                               frame->rect.height(), frame->palette, true,
//                               frame->lineWidth, frame->midLineWidth);
                painter->drawRoundedRect(QRectF(frame->rect).adjusted(0.5, 0.5, -0.5, -0.5), 4, 4);
            }
            painter->restore();
        }
        break;
    case PE_FrameLineEdit:
        {
            const QStyleOptionFrame *frameOption = qstyleoption_cast<const QStyleOptionFrame *>(opt);
            if (!frameOption) {
                return;
            }

            painter->save();
            painter->setRenderHint(QPainter::Antialiasing);
            const bool bHasFocus = QStyle::State_HasFocus & frameOption->state;
            bool MouseOver = frameOption->state & QStyle::State_MouseOver;
            // 设置画笔
            QPen pen(QColor(bHasFocus||MouseOver?frameOption->palette.color(QPalette::Active, QPalette::Highlight):frameOption->palette.color(QPalette::Inactive, QPalette::WindowText)), 1);
            QColor colorTmp = frameOption->palette.color(QPalette::Inactive, QPalette::PlaceholderText);
            painter->setPen(pen);
            painter->setBrush(Qt::NoBrush);
            QPainterPath path;
            //path.addRoundedRect(rect.adjusted(1,1,-1,-1), 10, 10);

            // 填充内部（确保边缘平滑）
            //p->drawPath(path);
            // 绘制线条
            painter->drawRoundedRect(QRectF(opt->rect).adjusted(0.5, 0.5, -0.5, -0.5), 6, 6);

            painter->restore();
        }
        DEBUG_OUT_PUT("PE_FrameLineEdit");
        break;
    case PE_FrameMenu:
    {
        const QStyleOptionFrame *frameOpt = qstyleoption_cast<const QStyleOptionFrame *>(opt);
        if (frameOpt)
        {
            painter->save();
            //p->fillRect(opt->rect, Qt::transparent);
            QPen pen(frameOpt->palette.color(QPalette::ButtonText).lighter(180), 1);
            painter->setPen(pen);
            painter->setBrush(Qt::NoBrush);
            painter->setRenderHint(QPainter::Antialiasing, true);
            double adustVal = 1.0/2.0;
            painter->drawRect(QRectF(frameOpt->rect).adjusted(adustVal, adustVal, -adustVal, -adustVal));
            painter->restore();
        }
    }
        DEBUG_OUT_PUT("PE_FrameMenu");
        break;
    case PE_FrameStatusBarItem:
        DEBUG_OUT_PUT("PE_FrameStatusBarItem");
        break;
    case PE_FrameTabWidget:
    {
//        const QStyleOptionTabWidgetFrame *frameOption = qstyleoption_cast<const QStyleOptionTabWidgetFrame *>(opt);
//        if (frameOption)
//        {
//            p->save();
//            p->setRenderHint(QPainter::Antialiasing);
//            QPen pen(opt->palette.windowText().color(), frameOption->lineWidth);
//            p->setPen(pen);
//            p->drawRoundedRect(opt->rect.adjusted(1, 1, -1, -1), 6, 6);
//            p->restore();
//        }
        DEBUG_OUT_PUT("PE_FrameTabWidget");
        break;
    }
    case PE_FrameWindow:
        DEBUG_OUT_PUT("PE_FrameWindow");
        break;
    case PE_FrameButtonBevel:
        DEBUG_OUT_PUT("PE_FrameButtonBevel");
        break;
    case PE_FrameButtonTool:
        DEBUG_OUT_PUT("PE_FrameButtonTool");
        break;
    case PE_FrameTabBarBase:
        DEBUG_OUT_PUT("PE_FrameTabBarBase");
        break;
    case PE_PanelButtonCommand:
    {
//        DrawDefaultButton(opt, p);
        DrawStandardButton(opt, painter, w);
//        p->setPen(QPen(QColor(50, 50, 50), 2));
//        p->drawLine(10, 10 + 100/2, 10 + 100/2, 10 + 100/2);
//        p->drawLine(10 + 100/2, 10 + 100/2, 10 + 100/2, 10 + 100);

//        // Draw lighter border on right and top
//        p->setPen(QPen(QColor(150, 150, 150), 2));
//        p->drawLine(10 + 100/2, 10, 10 + 100, 10 + 100/2);
//        p->drawLine(10 + 100/2, 10 + 1, 10 + 1, 10 + 100/2);

//        p->restore();
//        qDrawShadePanel(p, opt->rect, opt->palette,
//                        opt->state & (State_Sunken | State_On), 1,
//                        &palette.brush(QPalette::Button));
        DEBUG_OUT_PUT("PE_PanelButtonCommand");
        break;
    }
    case PE_PanelButtonBevel:
        DEBUG_OUT_PUT("PE_PanelButtonBevel");
        break;
    case PE_PanelButtonTool:
    {
        painter->save();
        bool bMouseOver = opt->state & QStyle::State_MouseOver;
        bool bDown = (opt->state & QStyle::State_On) || (opt->state & QStyle::State_Sunken);
        QColor penColor = opt->palette.color(QPalette::ButtonText);
        QColor brushColor = opt->palette.color(QPalette::Button);
        if (bMouseOver)
        {
            penColor = opt->palette.color(QPalette::Highlight);
            brushColor = opt->palette.color(QPalette::Highlight);
        }
        else if (bDown)
        {
            penColor = opt->palette.color(QPalette::Highlight);
        }
        QPen pen(penColor, 1);
        QBrush brush(brushColor);
        painter->setPen(pen);
        painter->setBrush(brush);
        painter->setRenderHint(QPainter::Antialiasing, true);
        double adjustVal = 0.5;
        painter->drawRoundedRect(QRectF(rect).adjusted(adjustVal,adjustVal,-adjustVal,-adjustVal), 4, 4);
        painter->restore();
    }
        break;
        DEBUG_OUT_PUT("PE_PanelButtonTool");
        break;
    case PE_PanelMenuBar:
        DEBUG_OUT_PUT("PE_PanelMenuBar");
        break;
    case PE_PanelToolBar:
        DEBUG_OUT_PUT("PE_PanelToolBar");
        break;
    case PE_PanelLineEdit:
    {
        if (const QStyleOptionFrame *panel = qstyleoption_cast<const QStyleOptionFrame *>(opt)) {
            //const bool bHasFocus = QStyle::State_HasFocus & panel->state;
//            p->fillRect(panel->rect.adjusted(panel->lineWidth, panel->lineWidth, -panel->lineWidth, -panel->lineWidth),
//                        panel->palette.brush(!bHasFocus ? QPalette::AlternateBase : QPalette::Base));
            painter->save();
            painter->setRenderHint(QPainter::Antialiasing);
            QPen pen(Qt::NoPen);
            painter->setPen(pen);
            painter->setBrush(panel->palette.color(QPalette::Active, QPalette::Window));
            painter->drawRoundedRect(panel->rect.adjusted(panel->lineWidth, panel->lineWidth, -panel->lineWidth, -panel->lineWidth), 6, 6);
            //p->drawText(panel->rect, panel.)
            painter->restore();
            if (panel->lineWidth > 0)
                /*proxy()->*/drawPrimitive(PE_FrameLineEdit, panel, painter, w);
        }
        DEBUG_OUT_PUT("PE_PanelLineEdit");
        break;
    }
    case PE_IndicatorArrowDown:
    case PE_IndicatorArrowLeft:
    case PE_IndicatorArrowRight:
    case PE_IndicatorArrowUp:
    {
        painter->save();
        painter->setRenderHint(QPainter::Antialiasing, true);
        if (opt->rect.width() <= 1 || opt->rect.height() <= 1)
            break;
//        const int rectWidth = opt->rect.width();
//        const int rectHeight = opt->rect.height();
        QRect arrowRect = opt->rect.adjusted(-1, -1, 1, 1);
        QColor arrowColor = opt->palette.buttonText().color();
        //arrowColor.setAlpha(160);
        QPen pen = painter->pen();
        pen.setColor(arrowColor);
        pen.setWidth(2);
        pen.setJoinStyle(Qt::RoundJoin);
        painter->setPen(QPen(arrowColor, 2));
        QPointF pt[3] = {QPointF(0, 0), QPointF(0, 0), QPointF(0, 0)};
        QPainterPath path;
        int arrowSize = 6;
        switch (pe) {
        case PE_IndicatorArrowDown:
        {
            //绘制向下的箭头
            int centerX = arrowRect.center().x();
            int centerY = arrowRect.center().y() + 1;
            pt[0] = QPointF(centerX - arrowSize/2, centerY - arrowSize/3);
            pt[1] = QPointF(centerX, centerY + arrowSize/3);
            pt[2] = QPointF(centerX + arrowSize/2, centerY - arrowSize/3);
            break;
        }
        case PE_IndicatorArrowRight:
        {
            //绘制向右的箭头
            int centerX = arrowRect.center().x() + 1;
            int centerY = arrowRect.center().y();
            pt[0] = QPointF(centerX - arrowSize/3, centerY - arrowSize/2);
            pt[1] = QPointF(centerX + arrowSize/3, centerY);
            pt[2] = QPointF(centerX - arrowSize/3, centerY + arrowSize/2);
            break;
        }
        case PE_IndicatorArrowLeft:
        {
            //绘制向左的箭头
            int centerX = arrowRect.center().x() - 1;
            int centerY = arrowRect.center().y();
            pt[0] = QPointF(centerX + arrowSize/3, centerY - arrowSize/2);
            pt[1] = QPointF(centerX - arrowSize/3, centerY);
            pt[2] = QPointF(centerX + arrowSize/3, centerY + arrowSize/2);
        }
            break;
        default://上
        {
            //绘制向上的箭头
            int centerX = arrowRect.center().x();
            int centerY = arrowRect.center().y() - 1;
            pt[0] = QPointF(centerX - arrowSize/2, centerY + arrowSize/3);
            pt[1] = QPointF(centerX, centerY - arrowSize/2);
            pt[2] = QPointF(centerX + arrowSize/2, centerY + arrowSize/3);
        }
            break;
        }
        path.moveTo(pt[0]);
        path.lineTo(pt[1]);
        path.lineTo(pt[2]);
        path.lineTo(pt[0]);
        painter->drawPath(path);
        painter->fillPath(path, QBrush(arrowColor));
        painter->restore();
    }
        break;
    case PE_IndicatorBranch:
    {
        painter->save();
        //绘制背景
        painter->save();
        painter->setPen(Qt::NoPen);
        painter->setBrush(opt->palette.color(QPalette::Window));
        painter->drawRect(opt->rect);
        painter->restore();
        static const int decoration_size = 9;
        int mid_h = opt->rect.x() + opt->rect.width() / 2;
        int mid_v = opt->rect.y() + opt->rect.height() / 2;
        int bef_h = mid_h;
        int bef_v = mid_v;
        int aft_h = mid_h;
        int aft_v = mid_v;
        if (opt->state & State_Children) {
            int delta = decoration_size / 2;
            bef_h -= delta;
            bef_v -= delta;
            aft_h += delta;
            aft_v += delta;
            //p->drawLine(bef_h + 2, bef_v + 4, bef_h + 6, bef_v + 4);
            if (!(opt->state & State_Open))
            {
                //p->drawLine(bef_h + 4, bef_v + 2, bef_h + 4, bef_v + 6);
                drawArrow(opt, painter, Qt::RightArrow);
            }
            else
            {
                drawArrow(opt, painter, Qt::DownArrow);
            }
            QPen oldPen = painter->pen();
            painter->setPen(opt->palette.dark().color());
            //p->drawRect(bef_h, bef_v, decoration_size - 1, decoration_size - 1);
            painter->setPen(oldPen);
        }
        QBrush brush(opt->palette.dark().color(), Qt::Dense4Pattern);
        if (opt->state & State_Item) {
            if (opt->direction == Qt::RightToLeft)
                painter->fillRect(opt->rect.left(), mid_v, bef_h - opt->rect.left(), 1, brush);
            else
                painter->fillRect(aft_h, mid_v, opt->rect.right() - aft_h + 1, 1, brush);
        }
        if (opt->state & State_Sibling)
            painter->fillRect(mid_h, aft_v, 1, opt->rect.bottom() - aft_v + 1, brush);
        if (opt->state & (State_Open | State_Children | State_Item | State_Sibling))
            painter->fillRect(mid_h, opt->rect.y(), 1, bef_v - opt->rect.y(), brush);
        painter->restore();
    }
        DEBUG_OUT_PUT("PE_IndicatorBranch");
        break;
    case PE_IndicatorButtonDropDown:
        DEBUG_OUT_PUT("PE_IndicatorButtonDropDown");
        break;
    case PE_IndicatorItemViewItemCheck:
        proxy()->drawPrimitive(PE_IndicatorCheckBox, opt, painter, w);
        break;
    case PE_IndicatorCheckBox:
    {
//        DrawFluentCheckBox(opt, p, w);
        //QPainter *painter = painter;
        const QStyleOption *option = opt;
        painter->save();
        painter->setRenderHint(QPainter::Antialiasing, true);
        if (const QStyleOptionButton *checkbox = qstyleoption_cast<const QStyleOptionButton*>(option)) {
            //painter->translate(0.5, 0.5);
            //rect = rect.adjusted(0, 0, 0, 0);
            if (checkbox->state & State_MouseOver) {
                rect.adjust(1, 1, -1, -1);
            }

            QColor pressedColor = mergedColors(option->palette.base().color(), option->palette.windowText().color(), 85);
            //painter->setBrush(Qt::NoBrush);

            // Gradient fill
            QLinearGradient gradient(rect.topLeft(), rect.bottomLeft());
            gradient.setColorAt(0, (state & State_Sunken) ? pressedColor : option->palette.base().color().darker(115));
            gradient.setColorAt(0.15, (state & State_Sunken) ? pressedColor : option->palette.base().color());
            gradient.setColorAt(1, (state & State_Sunken) ? pressedColor : option->palette.base().color());

            QColor checkMarkColor = option->palette.text().color().darker(120);
            const qreal checkMarkPadding = 1 + rect.width() * 0.1; // at least one pixel padding
            qreal penWidth = /*QStyleHelper::dpiScaled(1.5, dpi)*/2;
            if (checkbox->state & State_NoChange || checkbox->state & State_On)
            {
                //绘制背景
                painter->setPen(Qt::NoPen);
                //绘制选中背景
                painter->setBrush(option->palette.color(QPalette::Active, QPalette::Highlight));
                painter->drawRoundedRect(rect, 3, 3);
            }
            if (checkbox->state & State_NoChange) {
                //三态
                painter->setBrush(Qt::NoBrush);
                QPen pen;
                pen.setWidth(penWidth);
                pen.setColor(option->palette.color(QPalette::Active, QPalette::Window));
                painter->setPen(pen);
                QPainterPath path;
                const qreal rectHeight = rect.height(); // assuming height equals width
                const qreal rectWidth = rect.width(); // assuming height equals width
                path.moveTo(checkMarkPadding + rectWidth * 0.11, rectHeight * 0.5);
                path.lineTo(rectWidth - checkMarkPadding - 1, rectHeight * 0.5);
                painter->drawPath(path.translated(rect.topLeft()));
            } else if (checkbox->state & State_On) {
//                const qreal dpi = QStyleHelper::dpi(option);
//                penWidth = qMax<qreal>(penWidth, 0.13 * rect.height());
//                penWidth = qMin<qreal>(penWidth, 0.20 * rect.height());
                QPen checkPen = QPen(checkMarkColor, penWidth);
                checkMarkColor.setAlpha(210);
//                painter->translate(dpiScaled(-0.8, dpi), dpiScaled(0.5, dpi));
                // 绘制选中标志
                painter->setBrush(Qt::NoBrush);
                QPen pen;
                pen.setWidth(penWidth);
                pen.setColor(option->palette.color(QPalette::Active, QPalette::Window));
                painter->setPen(pen);
                QPainterPath path;
                const qreal rectHeight = rect.height(); // assuming height equals width
                path.moveTo(checkMarkPadding + rectHeight * 0.11, rectHeight * 0.47);
                path.lineTo(rectHeight * 0.5, rectHeight - checkMarkPadding-1);
                path.lineTo(rectHeight - checkMarkPadding - 1, checkMarkPadding + 1);
                painter->drawPath(path.translated(rect.topLeft()));
            } else if (checkbox->state & State_Off) {
                //绘制未选中背景
                //painter->setBrush((state & State_Sunken) ? QBrush(pressedColor) : gradient);
                painter->setBrush(Qt::NoBrush);
                QPen pen = painter->pen();
                pen.setWidth(2);
                pen.setColor(option->palette.color(QPalette::Active, QPalette::ButtonText));
                painter->setPen(option->palette.color(QPalette::Active, QPalette::ButtonText));

                if (option->state & State_HasFocus && option->state & State_KeyboardFocusChange)
                    painter->setPen(option->palette.color(QPalette::Active, QPalette::Highlight));
                //painter->drawRoundedRect(rect.adjusted(1,1,-1,-1), 3, 3);
                painter->drawRoundedRect(QRectF(rect).adjusted(0.5,0.5,0.5,0.5), 2, 2);
            }
        }
        painter->restore();
        break;
    }
    case PE_IndicatorDockWidgetResizeHandle:
        DEBUG_OUT_PUT("PE_IndicatorDockWidgetResizeHandle");
        break;
    case PE_IndicatorHeaderArrow:
        if (const QStyleOptionHeader *header = qstyleoption_cast<const QStyleOptionHeader *>(opt)) {
            if (header->sortIndicator != QStyleOptionHeader::None)
                drawArrow(header, painter,
                    (header->sortIndicator == QStyleOptionHeader::SortDown) ?
                    Qt::UpArrow : Qt::DownArrow);
        }
        DEBUG_OUT_PUT("PE_IndicatorHeaderArrow");
        break;
    case PE_IndicatorMenuCheckMark:
        DEBUG_OUT_PUT("PE_IndicatorMenuCheckMark");
        break;
    case PE_IndicatorProgressChunk:
        DEBUG_OUT_PUT("PE_IndicatorProgressChunk");
        break;
    case PE_IndicatorRadioButton:
    {
        painter->save();
        painter->setRenderHint(QPainter::Antialiasing);
        QRect ir = opt->rect;
        bool bMouseOver = opt->state & State_MouseOver;
        if (opt->state & (State_Sunken | State_On)) {
            ir.adjust(4, 4, -3, -3);
            //绘制底色
            painter->setPen(Qt::NoPen);
            painter->setBrush(opt->palette.highlight());
            QRect rectBase = opt->rect.adjusted(0,0,1,1);
            if (bMouseOver) {
                rectBase.adjust(1, 1, -1, -1);
            }
            painter->drawEllipse(rectBase);
            //绘制选中的中心圆点
            //p->setBrush(opt->palette.windowText().color().lighter(200));
            painter->setBrush(opt->palette.color(QPalette::Active, QPalette::Window));
            painter->drawEllipse(ir);
        }
        else
        {
            //未选中
            //绘制外圈
            QPen pen = painter->pen();
            pen.setWidth(2);
            pen.setColor(opt->palette.buttonText().color().darker(120));
            painter->setPen(pen);
            if (bMouseOver) {
                painter->setBrush(opt->palette.color(QPalette::Active, QPalette::Window));
            } else {
                painter->setBrush(opt->palette.color(QPalette::Inactive, QPalette::Window));
            }
            painter->drawEllipse(opt->rect.adjusted(1,1,0,0));
        }
        painter->restore();
        break;
    }
    case PE_IndicatorSpinUp:
    case PE_IndicatorSpinDown:
    {
        painter->save();
        painter->setPen(opt->palette.buttonText().color());
        painter->setBrush(opt->palette.buttonText());
        painter->setRenderHint(QPainter::Antialiasing);
        QRect arrowRect = opt->rect.adjusted(-1, -1, 1, 1);
        int arrowSize = 6;
        QPointF pt[3] = {QPointF(0, 0), QPointF(0, 0), QPointF(0, 0)};
        if (pe == PE_IndicatorSpinDown) {
            int centerX = arrowRect.center().x();
            int centerY = arrowRect.center().y() + 1;
            pt[0] = QPointF(centerX - arrowSize/2, centerY - arrowSize/3);
            pt[1] = QPointF(centerX, centerY + arrowSize/3);
            pt[2] = QPointF(centerX + arrowSize/2, centerY - arrowSize/3);
            drawArrow(opt, painter, Qt::DownArrow);
        } else {
            int centerX = arrowRect.center().x();
            int centerY = arrowRect.center().y() - 1;
            pt[0] = QPointF(centerX - arrowSize/2, centerY + arrowSize/3);
            pt[1] = QPointF(centerX, centerY - arrowSize/2);
            pt[2] = QPointF(centerX + arrowSize/2, centerY + arrowSize/3);
            drawArrow(opt, painter, Qt::UpArrow);
        }

        painter->restore();
        break;
    }
    case PE_IndicatorSpinMinus:
    case PE_IndicatorSpinPlus:
    {
        QRect r = opt->rect;
        int fw = proxy()->pixelMetric(PM_DefaultFrameWidth, opt, w);
        QRect br = r.adjusted(fw, fw, -fw, -fw);

        int offset = (opt->state & State_Sunken) ? 1 : 0;
        int step = (br.width() + 4) / 5;
        painter->fillRect(br.x() + offset, br.y() + offset +br.height() / 2 - step / 2,
                    br.width(), step,
                    opt->palette.buttonText());
        if (pe == PE_IndicatorSpinPlus)
            painter->fillRect(br.x() + br.width() / 2 - step / 2 + offset, br.y() + offset,
                        step, br.height(),
                        opt->palette.buttonText());

        break;
    }
    case PE_IndicatorToolBarHandle:
        DEBUG_OUT_PUT("PE_IndicatorToolBarHandle");
        break;
    case PE_IndicatorToolBarSeparator:
        DEBUG_OUT_PUT("PE_IndicatorToolBarSeparator");
        break;
    case PE_PanelTipLabel:
        DEBUG_OUT_PUT("PE_PanelTipLabel");
        break;
    case PE_IndicatorTabTear:
        DEBUG_OUT_PUT("PE_IndicatorTabTear");
        break;
    case PE_PanelScrollAreaCorner:
        DEBUG_OUT_PUT("PE_PanelScrollAreaCorner");
        break;
    case PE_Widget:
    {
        painter->setRenderHint(QPainter::Antialiasing);
        painter->setBrush(QColor(255, 255, 255));
        //p->setPen(QPen(QColor(100, 100, 100), 1));
        painter->setPen(Qt::NoPen);
        QRect rect = opt->rect;
        painter->drawRoundedRect(rect, 10, 10);
        DEBUG_OUT_PUT("PE_Widget");
        break;
    }
    case PE_IndicatorColumnViewArrow:
        if (const QStyleOptionViewItem *viewOpt = qstyleoption_cast<const QStyleOptionViewItem *>(opt)) {
            bool isOpen = viewOpt->state & State_Open;
            drawArrow(viewOpt, painter, isOpen ? Qt::DownArrow : Qt::RightArrow);
        }
        DEBUG_OUT_PUT("PE_IndicatorColumnViewArrow");
        break;
    case PE_IndicatorItemViewItemDrop:
        DEBUG_OUT_PUT("PE_IndicatorItemViewItemDrop");
        break;
    case PE_PanelItemViewItem://绘制listview/tableview项背景文字
        if (const QStyleOptionViewItem *vopt = qstyleoption_cast<const QStyleOptionViewItem*>(opt)) {
            bool isSelected = vopt->state & State_Selected;
            bool isHovered = vopt->state & State_MouseOver;
//            bool hasFocus = vopt->state & State_HasFocus;

            bool isListView = qobject_cast<const QListView*>(vopt->widget);

            QRect rect = vopt->rect;
            painter->save();
            painter->setRenderHint(QPainter::Antialiasing, true);
            // 绘制背景
            if (isSelected) {
                bool bEdit = vopt->state & QStyle::State_Editing;
                if (isListView && !bEdit)
                {
                    QRect rectSelect(rect.adjusted(2, 2, 0, 0));
                    rectSelect.setWidth(3);
                    painter->save();
                    painter->setRenderHint(QPainter::Antialiasing, true);
                    painter->setPen(Qt::NoPen);
                    painter->setBrush(opt->palette.color(QPalette::Active, QPalette::Window).darker(105));
                    painter->drawRoundedRect(rect.adjusted(2, 2, -2, -2), 6, 6);
                    painter->setBrush(opt->palette.color(QPalette::Active, QPalette::Highlight));
                    painter->drawRoundedRect(rectSelect, 2, 2);
                    painter->restore();
                }
            } else if(isHovered) {
                painter->save();
                painter->setRenderHint(QPainter::Antialiasing, true);
                painter->setPen(Qt::NoPen);
                painter->setBrush(opt->palette.color(QPalette::Active, QPalette::Window).darker(105));
                painter->drawRoundedRect(rect.adjusted(1, 1, -1, -1), 4, 4);
                painter->restore();
            }else {
                painter->fillRect(rect, opt->palette.color(QPalette::Active, QPalette::Window));
            }

            // 绘制文本
            QString text = vopt->text;
            if (!text.isEmpty()) {
                if (isListView)
                {
                    painter->setPen(isSelected ?
                                        (opt->palette.color(QPalette::Active, QPalette::Highlight)) :
                                        (opt->palette.color(QPalette::Active, QPalette::WindowText)));
                }
                else
                {
                    painter->setPen(isSelected ?
                                        (opt->palette.color(QPalette::Active, QPalette::Window)) :
                                        (opt->palette.color(QPalette::Active, QPalette::WindowText)));
                }

                QRect textRect = rect.adjusted(6, 0, 0, 0);
                painter->drawText(textRect, Qt::AlignLeft | Qt::AlignVCenter, text);
            }
            painter->restore();
        }
        break;
    case PE_PanelItemViewRow:
        if (const QStyleOptionViewItem *vopt = qstyleoption_cast<const QStyleOptionViewItem *>(opt))
        {
            painter->save();
            painter->setRenderHint(QPainter::Antialiasing);
            painter->setPen(Qt::NoPen);
            painter->setBrush(opt->palette.color(QPalette::Highlight));
            painter->drawRoundedRect(opt->rect, 6, 6);
//            QPalette::ColorGroup cg = (w ? w->isEnabled() : (vopt->state & QStyle::State_Enabled))
//                                      ? QPalette::Normal : QPalette::Disabled;
//            if (cg == QPalette::Normal && !(vopt->state & QStyle::State_Active))
//                cg = QPalette::Inactive;

//            if ((vopt->state & QStyle::State_Selected) /*&&  proxy()->styleHint(QStyle::SH_ItemView_ShowDecorationSelected, opt, w)*/)
//                p->fillRect(vopt->rect, vopt->palette.brush(cg, QPalette::Highlight));
//            else if (vopt->features & QStyleOptionViewItem::Alternate)
//                p->fillRect(vopt->rect, vopt->palette.brush(cg, QPalette::AlternateBase));
            painter->restore();
        }
        break;
    case PE_PanelStatusBar:
        DEBUG_OUT_PUT("PE_PanelStatusBar");
        break;
    case PE_IndicatorTabClose:
        DEBUG_OUT_PUT("PE_IndicatorTabClose");
        break;
    case PE_PanelMenu:
    {
        painter->save();
        //p->fillRect(opt->rect, Qt::transparent);
        painter->setPen(Qt::NoPen);
        painter->setBrush(opt->palette.window());
        painter->setRenderHint(QPainter::Antialiasing, true);
//        const QPainterPath path = windowPanelPath(opt->rect);
//        p->drawPath(path);
        painter->drawRoundedRect(opt->rect.adjusted(1, 1, -1, -1), 6, 6);
        painter->restore();
     }
        DEBUG_OUT_PUT("PE_PanelMenu");
        break;
    case PE_IndicatorTabTearRight:
        DEBUG_OUT_PUT("PE_IndicatorTabTearRight");
        break;
    default:
        break;
    }
#endif
}

void CFluentStyle::drawControl(QStyle::ControlElement element, const QStyleOption *opt, QPainter *painter, const QWidget *widget) const
{
//    QProxyStyle::drawControl(element, opt, p, w);

#if 1
    switch (element) {
    case CE_PushButton:
        if (const QStyleOptionButton *btn = qstyleoption_cast<const QStyleOptionButton *>(opt)) {
            proxy()->drawControl(CE_PushButtonBevel, btn, painter, widget);
            QStyleOptionButton subopt = *btn;
            subopt.rect = subElementRect(SE_PushButtonContents, btn, widget);
            proxy()->drawControl(CE_PushButtonLabel, &subopt, painter, widget);
            if (btn->state & State_HasFocus) {
                QStyleOptionFocusRect fropt;
                fropt.QStyleOption::operator=(*btn);
                fropt.rect = subElementRect(SE_PushButtonFocusRect, btn, widget);
                proxy()->drawPrimitive(PE_FrameFocusRect, &fropt, painter, widget);
            }
        }
        break;
    case CE_PushButtonBevel:
        if (const QStyleOptionButton *btn = qstyleoption_cast<const QStyleOptionButton *>(opt)) {
            QRect br = btn->rect;
            int dbi = proxy()->pixelMetric(PM_ButtonDefaultIndicator, btn, widget);
            if (btn->features & QStyleOptionButton::DefaultButton)
                proxy()->drawPrimitive(PE_FrameDefaultButton, opt, painter, widget);
            if (btn->features & QStyleOptionButton::AutoDefaultButton)
                br.setCoords(br.left() + dbi, br.top() + dbi, br.right() - dbi, br.bottom() - dbi);
            if (!(btn->features & (QStyleOptionButton::Flat | QStyleOptionButton::CommandLinkButton))
                || btn->state & (State_Sunken | State_On)
                || (btn->features & QStyleOptionButton::CommandLinkButton && btn->state & State_MouseOver)) {
                QStyleOptionButton tmpBtn = *btn;
                tmpBtn.rect = br;
                proxy()->drawPrimitive(PE_PanelButtonCommand, &tmpBtn, painter, widget);
            }
            if (btn->features & QStyleOptionButton::HasMenu) {
                int mbi = proxy()->pixelMetric(PM_MenuButtonIndicator, btn, widget);
                QRect ir = btn->rect;
                QStyleOptionButton newBtn = *btn;
                newBtn.rect = QRect(ir.right() - mbi + 2, ir.height()/2 - mbi/2 + 3, mbi - 6, mbi - 6);
                newBtn.rect = visualRect(btn->direction, br, newBtn.rect);
                proxy()->drawPrimitive(PE_IndicatorArrowDown, &newBtn, painter, widget);
            }
        }
        break;
    case CE_PushButtonLabel:
        if (const QStyleOptionButton *button = qstyleoption_cast<const QStyleOptionButton *>(opt)) {
            QRect textRect = button->rect;
            uint tf = Qt::AlignVCenter | Qt::TextShowMnemonic;
            if (!proxy()->styleHint(SH_UnderlineShortcut, button, widget))
                tf |= Qt::TextHideMnemonic;

            if (!button->icon.isNull()) {
                //Center both icon and text
                QIcon::Mode mode = button->state & State_Enabled ? QIcon::Normal : QIcon::Disabled;
                if (mode == QIcon::Normal && button->state & State_HasFocus)
                    mode = QIcon::Active;
                QIcon::State state = QIcon::Off;
                if (button->state & State_On)
                    state = QIcon::On;

                QPixmap pixmap = button->icon.pixmap(widget ? widget->window()->windowHandle() : 0, button->iconSize, mode, state);
                int pixmapWidth = pixmap.width() / pixmap.devicePixelRatio();
                int pixmapHeight = pixmap.height() / pixmap.devicePixelRatio();
                int labelWidth = pixmapWidth;
                int labelHeight = pixmapHeight;
                int iconSpacing = 4;//### 4 is currently hardcoded in QPushButton::sizeHint()
                if (!button->text.isEmpty()) {
                    int textWidth = button->fontMetrics.boundingRect(opt->rect, tf, button->text).width();
                    labelWidth += (textWidth + iconSpacing * 2);
                }

                QRect iconRect = QRect(textRect.x() + (textRect.width() - labelWidth) / 2,
                                       textRect.y() + (textRect.height() - labelHeight) / 2,
                                       pixmapWidth, pixmapHeight);

                iconRect = visualRect(button->direction, textRect, iconRect);

                if (button->direction == Qt::RightToLeft) {
                    tf |= Qt::AlignRight;
                    textRect.setRight(iconRect.left() - iconSpacing);
                } else {
                    tf |= Qt::AlignLeft; //left align, we adjust the text-rect instead
                    textRect.setLeft(iconRect.left() + iconRect.width() + iconSpacing);
                }

                if (button->state & (State_On | State_Sunken))
                    iconRect.translate(proxy()->pixelMetric(PM_ButtonShiftHorizontal, opt, widget),
                                       proxy()->pixelMetric(PM_ButtonShiftVertical, opt, widget));
                painter->drawPixmap(iconRect, pixmap);
            } else {
                tf |= Qt::AlignHCenter;
            }
            if (button->state & (State_On | State_Sunken))
                textRect.translate(proxy()->pixelMetric(PM_ButtonShiftHorizontal, opt, widget),
                             proxy()->pixelMetric(PM_ButtonShiftVertical, opt, widget));

            if (button->features & QStyleOptionButton::HasMenu) {
                int indicatorSize = proxy()->pixelMetric(PM_MenuButtonIndicator, button, widget);
                if (button->direction == Qt::LeftToRight)
                    textRect = textRect.adjusted(0, 0, -indicatorSize, 0);
                else
                    textRect = textRect.adjusted(indicatorSize, 0, 0, 0);
            }
            painter->save();
            QPalette palette = button->palette;
            if (button->features & QStyleOptionButton::DefaultButton)
            {
                palette.setColor(QPalette::ButtonText, DefaultButtonLabelColor());
            }
            else
            {
                palette.setColor(QPalette::ButtonText, StandardButtonLabelColor());
            }
            proxy()->drawItemText(painter, textRect, tf, palette, (button->state & State_Enabled),
                         button->text, QPalette::ButtonText);
            painter->restore();
        }
        DEBUG_OUT_PUT("CE_PushButtonLabel");
        break;
    case CE_RadioButton:
    case CE_CheckBox:
        if (const QStyleOptionButton *btn = qstyleoption_cast<const QStyleOptionButton *>(opt)) {
            bool isRadio = (element == CE_RadioButton);
            QStyleOptionButton subopt = *btn;
            subopt.rect = subElementRect(isRadio ? SE_RadioButtonIndicator
                                                 : SE_CheckBoxIndicator, btn, widget);
            proxy()->drawPrimitive(isRadio ? PE_IndicatorRadioButton : PE_IndicatorCheckBox,
                          &subopt, painter, widget);
            subopt.rect = subElementRect(isRadio ? SE_RadioButtonContents
                                                 : SE_CheckBoxContents, btn, widget);
            proxy()->drawControl(isRadio ? CE_RadioButtonLabel : CE_CheckBoxLabel, &subopt, painter, widget);
            if (btn->state & State_HasFocus) {
                QStyleOptionFocusRect fropt;
                fropt.QStyleOption::operator=(*btn);
                fropt.rect = subElementRect(isRadio ? SE_RadioButtonFocusRect
                                                    : SE_CheckBoxFocusRect, btn, widget);
                proxy()->drawPrimitive(PE_FrameFocusRect, &fropt, painter, widget);
            }
        }
        DEBUG_OUT_PUT("CE_CheckBox");
        break;
    case CE_CheckBoxLabel:
        if (const QStyleOptionButton *btn = qstyleoption_cast<const QStyleOptionButton *>(opt)) {
            uint alignment = visualAlignment(btn->direction, Qt::AlignLeft | Qt::AlignVCenter);

            if (!proxy()->styleHint(SH_UnderlineShortcut, btn, widget))
                alignment |= Qt::TextHideMnemonic;
            QPixmap pix;
            QRect textRect = btn->rect;
            if (!btn->icon.isNull()) {
                pix = btn->icon.pixmap(GetWindow(widget), btn->iconSize, btn->state & State_Enabled ? QIcon::Normal : QIcon::Disabled);
                proxy()->drawItemPixmap(painter, btn->rect, alignment, pix);
                if (btn->direction == Qt::RightToLeft)
                    textRect.setRight(textRect.right() - btn->iconSize.width() - 4);
                else
                    textRect.setLeft(textRect.left() + btn->iconSize.width() + 4);
            }
            if (!btn->text.isEmpty()){
                proxy()->drawItemText(painter, textRect, alignment | Qt::TextShowMnemonic,
                    btn->palette, btn->state & State_Enabled, btn->text, QPalette::WindowText);
            }
        }
        DEBUG_OUT_PUT("CE_CheckBoxLabel");
        break;
    case CE_RadioButtonLabel:
        if (const QStyleOptionButton *btn = qstyleoption_cast<const QStyleOptionButton *>(opt)) {
            uint alignment = visualAlignment(btn->direction, Qt::AlignLeft | Qt::AlignVCenter);

            if (!proxy()->styleHint(SH_UnderlineShortcut, btn, widget))
                alignment |= Qt::TextHideMnemonic;
            QPixmap pix;
            QRect textRect = btn->rect;
            if (!btn->icon.isNull()) {
                pix = btn->icon.pixmap(qt_getWindow(widget), btn->iconSize, btn->state & State_Enabled ? QIcon::Normal : QIcon::Disabled);
                proxy()->drawItemPixmap(painter, btn->rect, alignment, pix);
                if (btn->direction == Qt::RightToLeft)
                    textRect.setRight(textRect.right() - btn->iconSize.width() - 4);
                else
                    textRect.setLeft(textRect.left() + btn->iconSize.width() + 4);
            }
            if (!btn->text.isEmpty()){
                proxy()->drawItemText(painter, textRect, alignment | Qt::TextShowMnemonic,
                                      btn->palette, btn->state & State_Enabled, btn->text, QPalette::WindowText);
            }
        }
        DEBUG_OUT_PUT("CE_RadioButtonLabel");
        break;
    case CE_TabBarTab://tabwidget
        if (const QStyleOptionTab *tab = qstyleoption_cast<const QStyleOptionTab *>(opt)) {
            proxy()->drawControl(CE_TabBarTabShape, tab, painter, widget);
            proxy()->drawControl(CE_TabBarTabLabel, tab, painter, widget);
        }
        break;
    case CE_TabBarTabShape://tabwidget
        if (const QStyleOptionTab *tab = qstyleoption_cast<const QStyleOptionTab *>(opt)) {
            painter->save();

            QRect rect(tab->rect);
            bool selected = tab->state & State_Selected;
            bool onlyOne = tab->position == QStyleOptionTab::OnlyOneTab;
            int tabOverlap = onlyOne ? 0 : proxy()->pixelMetric(PM_TabBarTabOverlap, opt, widget);

            if (!selected) {
                switch (tab->shape) {
                case QTabBar::TriangularNorth:
                    rect.adjust(0, 0, 0, -tabOverlap);
                    if(!selected)
                        rect.adjust(1, 1, -1, 0);
                    break;
                case QTabBar::TriangularSouth:
                    rect.adjust(0, tabOverlap, 0, 0);
                    if(!selected)
                        rect.adjust(1, 0, -1, -1);
                    break;
                case QTabBar::TriangularEast:
                    rect.adjust(tabOverlap, 0, 0, 0);
                    if(!selected)
                        rect.adjust(0, 1, -1, -1);
                    break;
                case QTabBar::TriangularWest:
                    rect.adjust(0, 0, -tabOverlap, 0);
                    if(!selected)
                        rect.adjust(1, 1, 0, -1);
                    break;
                default:
                    break;
                }
            }

            painter->setPen(QPen(tab->palette.windowText(), 0));
            if (selected) {
                painter->setBrush(tab->palette.base());
            } else {
                if (widget && widget->parentWidget())
                    painter->setBrush(widget->parentWidget()->palette().window().color().darker(110));
                else
                    painter->setBrush(tab->palette.window().color().darker(110));
            }

            if (tab->shape >= QTabBar::RoundedNorth && tab->shape <= QTabBar::RoundedEast)
            {
                QRect selectFlag;
//                const QPointF center = opt->rect.center();
                switch(tab->shape)
                {
                    case QTabBar::RoundedNorth:
                        selectFlag.setTop(opt->rect.bottom()-5);
                        selectFlag.setBottom(opt->rect.bottom()-3);
                        selectFlag.setLeft(opt->rect.center().x() - opt->rect.width()*0.3);
                        selectFlag.setWidth(opt->rect.width()*0.6);
                        break;
                    case QTabBar::RoundedSouth:
                        selectFlag.setTop(opt->rect.top()+3);
                        selectFlag.setBottom(opt->rect.top()+5);
                        selectFlag.setLeft(opt->rect.center().x() - opt->rect.width()*0.3);
                        selectFlag.setWidth(opt->rect.width()*0.6);
                        break;
                    case QTabBar::RoundedWest:
                        selectFlag.setLeft(opt->rect.right()-5);
                        selectFlag.setRight(opt->rect.right()-3);
                        selectFlag.setTop(opt->rect.center().y() - opt->rect.height()*0.3);
                        selectFlag.setHeight(opt->rect.height()*0.6);
                        break;
                    case QTabBar::RoundedEast:
                        selectFlag.setRight(opt->rect.left()+5);
                        selectFlag.setLeft(opt->rect.left()+3);
                        selectFlag.setTop(opt->rect.center().y() - opt->rect.height()*0.3);
                        selectFlag.setHeight(opt->rect.height()*0.6);
                        break;
                    default:
                    break;
                }
                //绘制边框
                painter->setRenderHint(QPainter::Antialiasing);
                painter->setPen(Qt::NoPen);
                painter->drawRoundedRect(tab->rect, 10, 10);
                //绘制选中的项
                if (selected /*&& selectFlag.isValid()*/)
                {
                    painter->save();
                    painter->setRenderHint(QPainter::Antialiasing);
                    painter->setPen(Qt::NoPen);
                    painter->setBrush(opt->palette.color(QPalette::Active, QPalette::Highlight));
                    //int nRadiu = selectFlag.height() * 0.5;
                    int nRadiu = 1;
                    painter->drawRoundedRect(selectFlag, nRadiu, nRadiu);
                    painter->restore();
                }
            }
            else
            {
                int y;
                int x;
                QPolygon a(10);
                switch (tab->shape) {
                case QTabBar::TriangularNorth:
                case QTabBar::TriangularSouth: {
                    a.setPoint(0, 0, -1);
                    a.setPoint(1, 0, 0);
                    y = rect.height() - 2;
                    x = y / 3;
                    a.setPoint(2, x++, y - 1);
                    ++x;
                    a.setPoint(3, x++, y++);
                    a.setPoint(4, x, y);

                    int i;
                    int right = rect.width() - 1;
                    for (i = 0; i < 5; ++i)
                        a.setPoint(9 - i, right - a.point(i).x(), a.point(i).y());
                    if (tab->shape == QTabBar::TriangularNorth)
                        for (i = 0; i < 10; ++i)
                            a.setPoint(i, a.point(i).x(), rect.height() - 1 - a.point(i).y());

                    a.translate(rect.left(), rect.top());
                    painter->setRenderHint(QPainter::Antialiasing);
                    //p->translate(0, 0.5);

                    QPainterPath path;
                    path.addPolygon(a);
                    painter->drawPath(path);
                    break; }
                case QTabBar::TriangularEast:
                case QTabBar::TriangularWest: {
                    a.setPoint(0, -1, 0);
                    a.setPoint(1, 0, 0);
                    x = rect.width() - 2;
                    y = x / 3;
                    a.setPoint(2, x - 1, y++);
                    ++y;
                    a.setPoint(3, x++, y++);
                    a.setPoint(4, x, y);
                    int i;
                    int bottom = rect.height() - 1;
                    for (i = 0; i < 5; ++i)
                        a.setPoint(9 - i, a.point(i).x(), bottom - a.point(i).y());
                    if (tab->shape == QTabBar::TriangularWest)
                        for (i = 0; i < 10; ++i)
                            a.setPoint(i, rect.width() - 1 - a.point(i).x(), a.point(i).y());
                    a.translate(rect.left(), rect.top());
                    painter->setRenderHint(QPainter::Antialiasing);
                    painter->translate(0.5, 0);
                    QPainterPath path;
                    path.addPolygon(a);
                    painter->drawPath(path);
                    break; }
                default:
                    break;
                }
            }
            painter->restore();
        }
        break;
    case CE_TabBarTabLabel://tabwidget
        if (const QStyleOptionTab *tab = qstyleoption_cast<const QStyleOptionTab *>(opt)) {
            QRect tr = tab->rect;
            bool verticalTabs = tab->shape == QTabBar::RoundedEast
                                || tab->shape == QTabBar::RoundedWest
                                || tab->shape == QTabBar::TriangularEast
                                || tab->shape == QTabBar::TriangularWest;

            int alignment = Qt::AlignCenter | Qt::TextShowMnemonic;
            if (!proxy()->styleHint(SH_UnderlineShortcut, opt, widget))
                alignment |= Qt::TextHideMnemonic;

            if (verticalTabs) {
                painter->save();
                int newX, newY, newRot;
                if (tab->shape == QTabBar::RoundedEast || tab->shape == QTabBar::TriangularEast) {
                    newX = tr.width() + tr.x();
                    newY = tr.y();
                    newRot = 90;
                } else {
                    newX = tr.x();
                    newY = tr.y() + tr.height();
                    newRot = -90;
                }
                QTransform m = QTransform::fromTranslate(newX, newY);
                m.rotate(newRot);
                painter->setTransform(m, true);
            }
            QRect iconRect;
            tabLayout(tab, widget, &tr, &iconRect);
            tr = proxy()->subElementRect(SE_TabBarTabText, opt, widget); //we compute tr twice because the style may override subElementRect

            if (!tab->icon.isNull()) {
                QPixmap tabIcon = tab->icon.pixmap(qt_getWindow(widget), tab->iconSize,
                                                   (tab->state & State_Enabled) ? QIcon::Normal
                                                                                : QIcon::Disabled,
                                                   (tab->state & State_Selected) ? QIcon::On
                                                                                 : QIcon::Off);
                painter->drawPixmap(iconRect.x(), iconRect.y(), tabIcon);
            }

            proxy()->drawItemText(painter, tr, alignment, tab->palette, tab->state & State_Enabled, tab->text, QPalette::WindowText);
            if (verticalTabs)
                painter->restore();

            if (tab->state & State_HasFocus) {
                const int OFFSET = 1 + pixelMetric(PM_DefaultFrameWidth);

                int x1, x2;
                x1 = tab->rect.left();
                x2 = tab->rect.right() - 1;

                QStyleOptionFocusRect fropt;
                fropt.QStyleOption::operator=(*tab);
                fropt.rect.setRect(x1 + 1 + OFFSET, tab->rect.y() + OFFSET,
                                   x2 - x1 - 2*OFFSET, tab->rect.height() - 2*OFFSET);
                drawPrimitive(PE_FrameFocusRect, &fropt, painter, widget);
            }
        }
        break;
    case CE_ProgressBar:
        if (const QStyleOptionProgressBar *pb
                = qstyleoption_cast<const QStyleOptionProgressBar *>(opt)) {
            QStyleOptionProgressBar subopt = *pb;
            subopt.rect = subElementRect(SE_ProgressBarGroove, pb, widget);
            proxy()->drawControl(CE_ProgressBarGroove, &subopt, painter, widget);
            subopt.rect = subElementRect(SE_ProgressBarContents, pb, widget);
            proxy()->drawControl(CE_ProgressBarContents, &subopt, painter, widget);
            if (pb->textVisible) {
                subopt.rect = subElementRect(SE_ProgressBarLabel, pb, widget);
                proxy()->drawControl(CE_ProgressBarLabel, &subopt, painter, widget);
            }
        }
        break;
    case CE_ProgressBarGroove://进度条
        painter->save();
    {
        painter->setRenderHint(QPainter::Antialiasing, true);
        painter->setBrush(opt->palette.base());
        QPen pen(opt->palette.color(QPalette::Active, QPalette::WindowText).lighter(), 1);
        painter->setPen(pen);
        int nRadius = opt->rect.height() * 0.5;
        painter->drawRoundedRect(QRectF(opt->rect).adjusted(0.5, 0.5, -0.5, -0.5), nRadius, nRadius);
    }
        painter->restore();
        break;
    case CE_ProgressBarContents://进度条
        painter->save();
        painter->setRenderHint(QPainter::Antialiasing, true);
        painter->translate(0.5, 0.5);
        if (const QStyleOptionProgressBar *bar = qstyleoption_cast<const QStyleOptionProgressBar *>(opt)) {
            bool vertical = false;
            bool inverted = false;
            bool indeterminate = (bar->minimum == 0 && bar->maximum == 0);
            bool complete = bar->progress == bar->maximum;

            // Get extra style options if version 2
            vertical = (bar->orientation == Qt::Vertical);
            inverted = bar->invertedAppearance;
            QRect rect(opt->rect);
            // If the orientation is vertical, we use a transform to rotate
            // the progress bar 90 degrees clockwise.  This way we can use the
            // same rendering code for both orientations.
            if (vertical) {
                rect = QRect(rect.left(), rect.top(), rect.height(), rect.width()); // flip width and height
                QTransform m = QTransform::fromTranslate(rect.height()-1, -1.0);
                m.rotate(90.0);
                painter->setTransform(m, true);
            }

            int maxWidth = rect.width();
            const auto progress = qMax(bar->progress, bar->minimum); // workaround for bug in QProgressBar
            const auto totalSteps = qMax(Q_INT64_C(1), qint64(bar->maximum) - bar->minimum);
            const auto progressSteps = qint64(progress) - bar->minimum;
            const auto progressBarWidth = progressSteps * maxWidth / totalSteps;
            int width = indeterminate ? maxWidth : progressBarWidth;

            bool reverse = (!vertical && (bar->direction == Qt::RightToLeft)) || vertical;
            if (inverted)
                reverse = !reverse;

//            int step = 0;
            QRect progressBar;
            QColor highlight = (opt->palette.color(QPalette::Active, QPalette::Highlight));
            QColor highlightedoutline = highlight.darker(140);
            QColor outline(Qt::red);
            if (qGray(outline.rgb()) > qGray(highlightedoutline.rgb()))
                outline = highlightedoutline;

            if (!indeterminate) {
                QColor innerShadow(Qt::black);
                innerShadow.setAlpha(35);
                painter->setPen(innerShadow);
                if (!reverse) {
                    progressBar.setRect(rect.left(), rect.top(), width - 1, rect.height() - 1);
//                    if (!complete) {
//                        p->drawLine(progressBar.topRight() + QPoint(2, 1), progressBar.bottomRight() + QPoint(2, 0));
//                        p->setPen(QPen(highlight.darker(140)));
//                        p->drawLine(progressBar.topRight() + QPoint(1, 1), progressBar.bottomRight() + QPoint(1, 0));
//                    }
                } else {
                    progressBar.setRect(rect.right() - width - 1, rect.top(), width + 2, rect.height() - 1);
//                    if (!complete) {
//                        p->drawLine(progressBar.topLeft() + QPoint(-2, 1), progressBar.bottomLeft() + QPoint(-2, 0));
//                        p->setPen(QPen(highlight.darker(140)));
//                        p->drawLine(progressBar.topLeft() + QPoint(-1, 1), progressBar.bottomLeft() + QPoint(-1, 0));
//                    }
                }
            } else {
                progressBar.setRect(rect.left(), rect.top(), rect.width() - 1, rect.height() - 1);
            }

            if (indeterminate || bar->progress > bar->minimum) {

                painter->setPen(QPen(outline));

                QColor highlightedGradientStartColor = highlight.lighter(120);
                QColor highlightedGradientStopColor  = highlight;
                QLinearGradient gradient(rect.topLeft(), QPoint(rect.bottomLeft().x(), rect.bottomLeft().y()));
                gradient.setColorAt(0, highlightedGradientStartColor);
                gradient.setColorAt(1, highlightedGradientStopColor);

                painter->setBrush(gradient);

                painter->save();
                if (!complete && !indeterminate)
                    painter->setClipRect(progressBar.adjusted(-1, -1, -1, 1));
//                QRect fillRect = progressBar.adjusted( !indeterminate && !complete && reverse ? -2 : 0, 0,
//                                                       indeterminate || complete || reverse ? 0 : 2, 0);
                int nRadius = progressBar.height() * 0.5;
                if (bar->progress <= nRadius)
                {
                    QRect rectTmp(progressBar);
                    rectTmp.setTop(progressBar.center().y() - (bar->progress) * 0.5);
                    rectTmp.setBottom(progressBar.center().y() + (bar->progress) * 0.5);
                    ///rectTmp.setWidth(rectTmp.height());
                    //nRadius = rectTmp.height() * 0.5;
//                    p->drawArc(progressBar.adjusted(1, 1, -1, -1), )
                    painter->drawRoundedRect(rectTmp.adjusted(1, 1, -1, -1), nRadius, nRadius);
                }
                else
                {
                    painter->drawRoundedRect(QRectF(progressBar).adjusted(1, 1, -1, -1), nRadius, nRadius);
                }
                painter->restore();

                //p->setBrush(Qt::NoBrush);
                //p->setPen(QColor(255, 255, 255, 50));
                //p->drawRoundedRect(progressBar.adjusted(1, 1, -1, -1), 1, 1);

                if (!indeterminate) {
#if QT_CONFIG(animation)
                    //(const_cast<QFusionStylePrivate*>(d))->stopAnimation(option->styleObject);
#endif
                /*} else*/ {
                    highlightedGradientStartColor.setAlpha(120);
                    painter->setPen(QPen(highlightedGradientStartColor, 9.0));
                    painter->setClipRect(progressBar.adjusted(1, 1, -1, -1));
#if QT_CONFIG(animation)
//                if (QProgressStyleAnimation *animation = qobject_cast<QProgressStyleAnimation*>(d->animation(option->styleObject)))
//                    step = animation->animationStep() % 22;
//                else
//                    (const_cast<QFusionStylePrivate*>(d))->startAnimation(new QProgressStyleAnimation(d->animationFps, option->styleObject));
#endif
//                for (int x = progressBar.left() - rect.height(); x < rect.right() ; x += 22)
//                    p->drawLine(x + step, progressBar.bottom() + 1,
//                                      x + rect.height() + step, progressBar.top() - 2);
                }
            }
        }
    }
        painter->restore();
        break;
    case CE_ProgressBarLabel://进度条
        if (const QStyleOptionProgressBar *bar = qstyleoption_cast<const QStyleOptionProgressBar *>(opt)) {
            QRect leftRect;
            QRect rect = bar->rect;
            QColor textColor = opt->palette.text().color();
            QColor alternateTextColor = (opt->palette.color(QPalette::HighlightedText));

            painter->save();
            bool vertical = false, inverted = false;
            vertical = (bar->orientation == Qt::Vertical);
            inverted = bar->invertedAppearance;
            if (vertical)
                rect = QRect(rect.left(), rect.top(), rect.height(), rect.width()); // flip width and height
            const auto totalSteps = qMax(Q_INT64_C(1), qint64(bar->maximum) - bar->minimum);
            const auto progressSteps = qint64(bar->progress) - bar->minimum;
            const auto progressIndicatorPos = progressSteps * rect.width() / totalSteps;
            if (progressIndicatorPos >= 0 && progressIndicatorPos <= rect.width())
                leftRect = QRect(rect.left(), rect.top(), progressIndicatorPos, rect.height());
            if (vertical)
                leftRect.translate(rect.width() - progressIndicatorPos, 0);

            bool flip = (!vertical && (((bar->direction == Qt::RightToLeft) && !inverted) ||
                                       ((bar->direction == Qt::LeftToRight) && inverted)));

            QRegion rightRect = rect;
            rightRect = rightRect.subtracted(leftRect);
            painter->setClipRegion(rightRect);
            painter->setPen(flip ? alternateTextColor : textColor);
            painter->drawText(rect, bar->text, QTextOption(Qt::AlignAbsolute | Qt::AlignHCenter | Qt::AlignVCenter));
            if (!leftRect.isNull()) {
                painter->setPen(flip ? textColor : alternateTextColor);
                painter->setClipRect(leftRect);
                painter->drawText(rect, bar->text, QTextOption(Qt::AlignAbsolute | Qt::AlignHCenter | Qt::AlignVCenter));
            }
            painter->restore();
        }
        break;
    case CE_MenuItem://combobox下拉列表和menu
        if (const QStyleOptionMenuItem *menuitem = qstyleoption_cast<const QStyleOptionMenuItem *>(opt))
        {
            int tab = menuitem->tabWidth;
            bool dis = !(menuitem->state & State_Enabled);
            bool act = menuitem->state & State_Selected;
            bool checkable = menuitem->menuHasCheckableItems;
            bool checked = checkable ? menuitem->checked : false;

            // windows always has a check column, regardless whether we have an icon or not
            int checkcol = qMax(menuitem->maxIconWidth, 12);
            QRect rect(opt->rect);
            int x, y, width, height;
            rect.getRect(&x, &y, &width, &height);

            QBrush fill = menuitem->palette.brush(act ? QPalette::Highlight : QPalette::Button);
            if (act)
            {
                painter->save();
                QColor fillColor = fill.color();
                fillColor.setAlpha(200);
                fill.setColor(fillColor);
                painter->setRenderHint(QPainter::Antialiasing, true);
                painter->setPen(Qt::NoPen);
                painter->setBrush(fill);
                painter->drawRoundedRect(rect.adjusted(2, 2, -2, -2), 4, 4);
                painter->restore();
            }
            else
            {
                painter->fillRect(rect, fill);
            }

            if (element == CE_MenuEmptyArea)
                break;

            // draw separator -------------------------------------------------
            if (menuitem->menuItemType == QStyleOptionMenuItem::Separator) {
                int yoff = y-1 + height / 2;
                painter->setPen(menuitem->palette.dark().color());
                painter->drawLine(x, yoff, x+width, yoff);
                ++yoff;
                painter->setPen(menuitem->palette.light().color());
                painter->drawLine(x, yoff, x+width, yoff);
                return;
            }

            int xpos = x;

            // draw icon ------------------------------------------------------
            if (!menuitem->icon.isNull()) {
                QIcon::Mode mode = dis ? QIcon::Disabled : QIcon::Normal;
                if (act && !dis)
                    mode = QIcon::Active;
                QPixmap pixmap = checked ?
                                 menuitem->icon.pixmap(proxy()->pixelMetric(PM_SmallIconSize, opt, widget), mode, QIcon::On) :
                                 menuitem->icon.pixmap(proxy()->pixelMetric(PM_SmallIconSize, opt, widget), mode);
                const int pixw = pixmap.width() / pixmap.devicePixelRatio();
                const int pixh = pixmap.height() / pixmap.devicePixelRatio();
                QRect iconRect(0, 0, pixw, pixh);
                iconRect.moveCenter(QRect(xpos, y, checkcol, height).center());
                QRect vIconRect = visualRect(opt->direction, opt->rect, iconRect);
                painter->setPen(menuitem->palette.text().color());
                painter->setBrush(Qt::NoBrush);
                if (checked)
                    painter->drawRect(vIconRect.adjusted(-1, -1, 0, 0));
                painter->drawPixmap(vIconRect.topLeft(), pixmap);

            // draw checkmark -------------------------------------------------
            } else if (checked) {
                QStyleOptionMenuItem newMi = *menuitem;
                newMi.state = State_None;
                if (!dis)
                    newMi.state |= State_Enabled;
                if (act)
                    newMi.state |= State_On;

                QRect checkMarkRect = QRect(menuitem->rect.x() + windowsItemFrame,
                                            menuitem->rect.y() + windowsItemFrame,
                                            checkcol - 2 * windowsItemFrame,
                                            menuitem->rect.height() - 2*windowsItemFrame);
                newMi.rect = visualRect(opt->direction, opt->rect, checkMarkRect);
                proxy()->drawPrimitive(PE_IndicatorMenuCheckMark, &newMi, painter, widget);
            }

            QColor textColor = dis ? menuitem->palette.text().color() :
                               act ? menuitem->palette.highlightedText().color() : menuitem->palette.buttonText().color();
            painter->setPen(textColor);

            // draw text ------------------------------------------------------
            int xm = windowsItemFrame + checkcol + windowsItemHMargin;
            xpos = menuitem->rect.x() + xm;
            QRect textRect(xpos, y + windowsItemVMargin, width - xm - windowsRightBorder - tab + 1, height - 2 * windowsItemVMargin);
            QRect vTextRect = visualRect(opt->direction, opt->rect, textRect);
            QString s = menuitem->text;
            if (!s.isEmpty()) {
                painter->save();
                int t = s.indexOf(QLatin1Char('\t'));
                int text_flags = Qt::AlignVCenter|Qt::TextShowMnemonic | Qt::TextDontClip | Qt::TextSingleLine | Qt::AlignLeft;
                if (!proxy()->styleHint(SH_UnderlineShortcut, menuitem, widget))
                    text_flags |= Qt::TextHideMnemonic;
                // draw tab text ----------------
                if (t >= 0) {
                    QRect vShortcutRect = visualRect(opt->direction, opt->rect, QRect(textRect.topRight(), menuitem->rect.bottomRight()));
                    if (dis && !act && proxy()->styleHint(SH_EtchDisabledText, opt, widget)) {
                        painter->setPen(menuitem->palette.light().color());
                        painter->drawText(vShortcutRect.adjusted(1,1,1,1), text_flags, s.mid(t + 1));
                        painter->setPen(textColor);
                    }
                    painter->drawText(vShortcutRect, text_flags, s.mid(t + 1));
                    s = s.left(t);
                }
                QFont font = menuitem->font;
                if (menuitem->menuItemType == QStyleOptionMenuItem::DefaultItem)
                    font.setBold(true);
                painter->setFont(font);
                if (dis && !act && proxy()->styleHint(SH_EtchDisabledText, opt, widget)) {
                    painter->setPen(menuitem->palette.light().color());
                    painter->drawText(vTextRect.adjusted(1,1,1,1), text_flags, s.left(t));
                    painter->setPen(textColor);
                }
                painter->drawText(vTextRect, text_flags, s);
                painter->restore();
            }

            // draw sub menu arrow --------------------------------------------
            if (menuitem->menuItemType == QStyleOptionMenuItem::SubMenu) {
                int dim = (height - 2) / 2;
                PrimitiveElement arrow;
                arrow = (opt->direction == Qt::RightToLeft) ? PE_IndicatorArrowLeft : PE_IndicatorArrowRight;
                xpos = x + width - windowsArrowHMargin - windowsItemFrame - dim;
                QRect vSubMenuRect = visualRect(opt->direction, opt->rect, QRect(xpos, y + height / 2 - dim / 2, dim, dim));
                QStyleOptionMenuItem newMI = *menuitem;
                newMI.rect = vSubMenuRect;
                newMI.state = dis ? State_None : State_Enabled;
                if (act)
                    newMI.palette.setColor(QPalette::ButtonText, newMI.palette.highlightedText().color());
                proxy()->drawPrimitive(arrow, &newMI, painter, widget);
            }
        }
        DEBUG_OUT_PUT("CE_MenuItem");
        break;
    case CE_MenuScroller:
        DEBUG_OUT_PUT("CE_MenuScroller");
        break;
    case CE_MenuVMargin:
        DEBUG_OUT_PUT("CE_MenuVMargin");
        break;
    case CE_MenuHMargin:
        DEBUG_OUT_PUT("CE_MenuHMargin");
        break;
    case CE_MenuTearoff:
        DEBUG_OUT_PUT("CE_MenuTearoff");
        break;
    case CE_MenuEmptyArea:
    case CE_MenuBarItem:
        if (const QStyleOptionMenuItem *menuitem = qstyleoption_cast<const QStyleOptionMenuItem *>(opt))
        {
            int tab = menuitem->tabWidth;
            bool dis = !(menuitem->state & State_Enabled);
            bool act = menuitem->state & State_Selected;
            bool checkable = menuitem->menuHasCheckableItems;
            bool checked = checkable ? menuitem->checked : false;

            // windows always has a check column, regardless whether we have an icon or not
            int checkcol = qMax(menuitem->maxIconWidth, 12);
            QRect rect(opt->rect);
            int x, y, width, height;
            rect.getRect(&x, &y, &width, &height);

            QBrush fill = menuitem->palette.brush(act ? QPalette::Highlight : QPalette::Button);
            painter->fillRect(rect, fill);

            if (element == CE_MenuEmptyArea)
                break;

            // draw separator -------------------------------------------------
            if (menuitem->menuItemType == QStyleOptionMenuItem::Separator) {
                int yoff = y-1 + height / 2;
                painter->setPen(menuitem->palette.dark().color());
                painter->drawLine(x, yoff, x+width, yoff);
                ++yoff;
                painter->setPen(menuitem->palette.light().color());
                painter->drawLine(x, yoff, x+width, yoff);
                return;
            }

            int xpos = x;

            // draw icon ------------------------------------------------------
            if (!menuitem->icon.isNull()) {
                QIcon::Mode mode = dis ? QIcon::Disabled : QIcon::Normal;
                if (act && !dis)
                    mode = QIcon::Active;
                QPixmap pixmap = checked ?
                                 menuitem->icon.pixmap(proxy()->pixelMetric(PM_SmallIconSize, opt, widget), mode, QIcon::On) :
                                 menuitem->icon.pixmap(proxy()->pixelMetric(PM_SmallIconSize, opt, widget), mode);
                const int pixw = pixmap.width() / pixmap.devicePixelRatio();
                const int pixh = pixmap.height() / pixmap.devicePixelRatio();
                QRect iconRect(0, 0, pixw, pixh);
                iconRect.moveCenter(QRect(xpos, y, checkcol, height).center());
                QRect vIconRect = visualRect(opt->direction, opt->rect, iconRect);
                painter->setPen(menuitem->palette.text().color());
                painter->setBrush(Qt::NoBrush);
                if (checked)
                    painter->drawRect(vIconRect.adjusted(-1, -1, 0, 0));
                painter->drawPixmap(vIconRect.topLeft(), pixmap);

            // draw checkmark -------------------------------------------------
            } else if (checked) {
                QStyleOptionMenuItem newMi = *menuitem;
                newMi.state = State_None;
                if (!dis)
                    newMi.state |= State_Enabled;
                if (act)
                    newMi.state |= State_On;

                QRect checkMarkRect = QRect(menuitem->rect.x() + windowsItemFrame,
                                            menuitem->rect.y() + windowsItemFrame,
                                            checkcol - 2 * windowsItemFrame,
                                            menuitem->rect.height() - 2*windowsItemFrame);
                newMi.rect = visualRect(opt->direction, opt->rect, checkMarkRect);
                proxy()->drawPrimitive(PE_IndicatorMenuCheckMark, &newMi, painter, widget);
            }

            QColor textColor = dis ? menuitem->palette.text().color() :
                               act ? menuitem->palette.highlightedText().color() : menuitem->palette.buttonText().color();
            painter->setPen(textColor);

            // draw text ------------------------------------------------------
            int xm = windowsItemFrame + checkcol + windowsItemHMargin;
            xpos = menuitem->rect.x() + xm;
            QRect textRect(xpos, y + windowsItemVMargin, width - xm - windowsRightBorder - tab + 1, height - 2 * windowsItemVMargin);
            QRect vTextRect = visualRect(opt->direction, opt->rect, textRect);
            QString s = menuitem->text;
            if (!s.isEmpty()) {
                painter->save();
                int t = s.indexOf(QLatin1Char('\t'));
                int text_flags = Qt::AlignVCenter|Qt::TextShowMnemonic | Qt::TextDontClip | Qt::TextSingleLine | Qt::AlignLeft;
                if (!proxy()->styleHint(SH_UnderlineShortcut, menuitem, widget))
                    text_flags |= Qt::TextHideMnemonic;
                // draw tab text ----------------
                if (t >= 0) {
                    QRect vShortcutRect = visualRect(opt->direction, opt->rect, QRect(textRect.topRight(), menuitem->rect.bottomRight()));
                    if (dis && !act && proxy()->styleHint(SH_EtchDisabledText, opt, widget)) {
                        painter->setPen(menuitem->palette.light().color());
                        painter->drawText(vShortcutRect.adjusted(1,1,1,1), text_flags, s.mid(t + 1));
                        painter->setPen(textColor);
                    }
                    painter->drawText(vShortcutRect, text_flags, s.mid(t + 1));
                    s = s.left(t);
                }
                QFont font = menuitem->font;
                if (menuitem->menuItemType == QStyleOptionMenuItem::DefaultItem)
                    font.setBold(true);
                painter->setFont(font);
                if (dis && !act && proxy()->styleHint(SH_EtchDisabledText, opt, widget)) {
                    painter->setPen(menuitem->palette.light().color());
                    painter->drawText(vTextRect.adjusted(1,1,1,1), text_flags, s.left(t));
                    painter->setPen(textColor);
                }
                painter->drawText(vTextRect, text_flags, s);
                painter->restore();
            }

            // draw sub menu arrow --------------------------------------------
            if (menuitem->menuItemType == QStyleOptionMenuItem::SubMenu) {
                int dim = (height - 2) / 2;
                PrimitiveElement arrow;
                arrow = (opt->direction == Qt::RightToLeft) ? PE_IndicatorArrowLeft : PE_IndicatorArrowRight;
                xpos = x + width - windowsArrowHMargin - windowsItemFrame - dim;
                QRect vSubMenuRect = visualRect(opt->direction, opt->rect, QRect(xpos, y + height / 2 - dim / 2, dim, dim));
                QStyleOptionMenuItem newMI = *menuitem;
                newMI.rect = vSubMenuRect;
                newMI.state = dis ? State_None : State_Enabled;
                if (act)
                    newMI.palette.setColor(QPalette::ButtonText, newMI.palette.highlightedText().color());
                proxy()->drawPrimitive(arrow, &newMI, painter, widget);
            }
        }
        break;
    case CE_MenuBarEmptyArea:
        DEBUG_OUT_PUT("CE_MenuBarEmptyArea");
        break;
    case CE_ToolButtonLabel:
        if (const QStyleOptionToolButton *tb = qstyleoption_cast<const QStyleOptionToolButton *>(opt)) {
            QStyleOptionToolButton myTb = *tb;
            myTb.state &= ~State_AutoRaise;
            //if (QStyleHelper::hasAncestor(opt->styleObject, QAccessible::ToolBar))
            {
                QRect cr = tb->rect;
                int shiftX = 0;
                int shiftY = 0;
                bool needText = false;
                int alignment = 0;
                bool down = tb->state & (State_Sunken | State_On);
                if (down) {
                    shiftX = proxy()->pixelMetric(PM_ButtonShiftHorizontal, tb, widget);
                    shiftY = proxy()->pixelMetric(PM_ButtonShiftVertical, tb, widget);
                }
                // The down state is special for QToolButtons in a toolbar on the Mac
                // The text is a bit bolder and gets a drop shadow and the icons are also darkened.
                // This doesn't really fit into any particular case in QIcon, so we
                // do the majority of the work ourselves.
                if (!(tb->features & QStyleOptionToolButton::Arrow)) {
                    Qt::ToolButtonStyle tbstyle = tb->toolButtonStyle;
                    if (tb->icon.isNull() && !tb->text.isEmpty())
                        tbstyle = Qt::ToolButtonTextOnly;

                    switch (tbstyle) {
                    case Qt::ToolButtonTextOnly: {
                        needText = true;
                        alignment = Qt::AlignCenter;
                        break; }
                    case Qt::ToolButtonIconOnly:
                    case Qt::ToolButtonTextBesideIcon:
                    case Qt::ToolButtonTextUnderIcon: {
                        QRect pr = cr;
                        QIcon::Mode iconMode = (tb->state & State_Enabled) ? QIcon::Normal
                                                                            : QIcon::Disabled;
                        QIcon::State iconState = (tb->state & State_On) ? QIcon::On
                                                                         : QIcon::Off;
                        QPixmap pixmap = tb->icon.pixmap(GetWindow(widget),
                                                         tb->rect.size().boundedTo(tb->iconSize),
                                                         iconMode, iconState);

                        // Draw the text if it's needed.
                        if (tb->toolButtonStyle != Qt::ToolButtonIconOnly) {
                            needText = true;
                            if (tb->toolButtonStyle == Qt::ToolButtonTextUnderIcon) {
                                pr.setHeight(pixmap.size().height() / pixmap.devicePixelRatio() + 6);
                                cr.adjust(0, pr.bottom(), 0, -3);
                                alignment |= Qt::AlignCenter;
                            } else {
                                pr.setWidth(pixmap.width() / pixmap.devicePixelRatio() + 8);
                                cr.adjust(pr.right(), 0, 0, 0);
                                alignment |= Qt::AlignLeft | Qt::AlignVCenter;
                            }
                        }
                        if (opt->state & State_Sunken) {
//                            pr.translate(shiftX, shiftY);
//                            pixmap = darkenPixmap(pixmap);
                        }
                        proxy()->drawItemPixmap(painter, pr, Qt::AlignCenter, pixmap);
                        break; }
                    default:
                        Q_ASSERT(false);
                        break;
                    }

                    if (needText) {
                        QPalette pal = tb->palette;
                        QPalette::ColorRole role = QPalette::NoRole;
                        if (!proxy()->styleHint(SH_UnderlineShortcut, tb, widget))
                            alignment |= Qt::TextHideMnemonic;
                        if (down)
                            cr.translate(shiftX, shiftY);
                        if (tbstyle == Qt::ToolButtonTextOnly
                            || (tbstyle != Qt::ToolButtonTextOnly && !down)) {
                            QPen pen = painter->pen();
                            QColor light = down || /*isDarkMode()*/ /*? Qt::black :*/ Qt::white;
                            light.setAlphaF(0.375f);
                            painter->setPen(light);
                            painter->drawText(cr.adjusted(0, 1, 0, 1), alignment, tb->text);
                            painter->setPen(pen);
                            if (down && tbstyle == Qt::ToolButtonTextOnly) {
                                pal = QApplication::palette("QMenu");
                                pal.setCurrentColorGroup(tb->palette.currentColorGroup());
                                role = QPalette::HighlightedText;
                            }
                        }
                        proxy()->drawItemText(painter, cr, alignment, pal,
                                              tb->state & State_Enabled, tb->text, role);
                    }
                } else {
                    QCommonStyle::drawControl(element, &myTb, painter, widget);
                }
            } /*else
            {
                QCommonStyle::drawControl(ce, &myTb, p, w);
            }*/
        }
        DEBUG_OUT_PUT("CE_ToolButtonLabel");
        break;
    case CE_Header:
        if (const QStyleOptionHeader *header = qstyleoption_cast<const QStyleOptionHeader *>(opt)) {
            QRegion clipRegion = painter->clipRegion();
            painter->setClipRect(opt->rect);
            proxy()->drawControl(CE_HeaderSection, header, painter, widget);
            QStyleOptionHeader subopt = *header;
            subopt.rect = subElementRect(SE_HeaderLabel, header, widget);
            if (subopt.rect.isValid())
                proxy()->drawControl(CE_HeaderLabel, &subopt, painter, widget);
            if (header->sortIndicator != QStyleOptionHeader::None) {
                subopt.rect = subElementRect(SE_HeaderArrow, opt, widget);
                proxy()->drawPrimitive(PE_IndicatorHeaderArrow, &subopt, painter, widget);
            }
            painter->setClipRegion(clipRegion);
        }
        DEBUG_OUT_PUT("CE_Header");
        break;
    case CE_HeaderSection:
        if (const QStyleOptionHeader *header = qstyleoption_cast<const QStyleOptionHeader *>(opt)) {
            State flags = header->state;
            QRect ir = header->rect;


#if 0 // FIXME: What's this solving exactly?
            bool noVerticalHeader = true;
#if QT_CONFIG(tableview)
            if (w)
                if (const QTableView *table = qobject_cast<const QTableView *>(w->parentWidget()))
                    noVerticalHeader = !table->verticalHeader()->isVisible();
#endif

            const bool drawLeftBorder = header->orientation == Qt::Vertical
                    || header->position == QStyleOptionHeader::OnlyOneSection
                    || (header->position == QStyleOptionHeader::Beginning && noVerticalHeader);
#endif

            const bool pressed = (flags & State_Sunken) && !(flags & State_On);
            painter->fillRect(ir, pressed ? header->palette.dark() : header->palette.button());
            painter->setPen(QPen(header->palette.dark(), 1.0));
            if (header->orientation == Qt::Horizontal)
                painter->drawLine(QLineF(ir.right() + 0.5, ir.top() + /*headerSectionSeparatorInset*/2,
                                   ir.right() + 0.5, ir.bottom() - /*headerSectionSeparatorInset*/2));
            else
                painter->drawLine(QLineF(ir.left() + /*headerSectionSeparatorInset*/2, ir.bottom(),
                                   ir.right() - /*headerSectionSeparatorInset*/2, ir.bottom()));
        }
        DEBUG_OUT_PUT("CE_HeaderSection");
        break;
    case CE_HeaderLabel:
        if (const QStyleOptionHeader *header = qstyleoption_cast<const QStyleOptionHeader *>(opt)) {
            painter->save();
            QRect textr = header->rect;
            if (!header->icon.isNull()) {
                QIcon::Mode mode = QIcon::Disabled;
                if (opt->state & State_Enabled)
                    mode = QIcon::Normal;
                int iconExtent = proxy()->pixelMetric(PM_SmallIconSize);
                QPixmap pixmap = header->icon.pixmap(GetWindow(widget), QSize(iconExtent, iconExtent), mode);

                QRect pixr = header->rect;
                pixr.setY(header->rect.center().y() - (pixmap.height() / pixmap.devicePixelRatio() - 1) / 2);
                proxy()->drawItemPixmap(painter, pixr, Qt::AlignVCenter, pixmap);
                textr.translate(pixmap.width() / pixmap.devicePixelRatio() + 2, 0);
            }

            proxy()->drawItemText(painter, textr, header->textAlignment | Qt::AlignVCenter, header->palette,
                                       header->state & State_Enabled, header->text, QPalette::ButtonText);
            painter->restore();
        }
        DEBUG_OUT_PUT("CE_HeaderLabel");
        break;
    case CE_ToolBoxTab:
        DEBUG_OUT_PUT("CE_ToolBoxTab");
        break;
    case CE_SizeGrip:
        DEBUG_OUT_PUT("CE_SizeGrip");
        break;
    case CE_Splitter:
        DEBUG_OUT_PUT("CE_Splitter");
        break;
    case CE_RubberBand:
        DEBUG_OUT_PUT("CE_RubberBand");
        break;
    case CE_DockWidgetTitle:
        painter->save();
        if (const QStyleOptionDockWidget *dwOpt = qstyleoption_cast<const QStyleOptionDockWidget *>(opt)) {
            bool verticalTitleBar = dwOpt->verticalTitleBar;

            QRect titleRect = subElementRect(SE_DockWidgetTitleBarText, opt, widget);
            if (verticalTitleBar) {
                QRect rect = dwOpt->rect;
                QRect r = rect.transposed();
                titleRect = QRect(r.left() + rect.bottom()
                                  - titleRect.bottom(),
                                  r.top() + titleRect.left() - rect.left(),
                                  titleRect.height(), titleRect.width());

                painter->translate(r.left(), r.top() + r.width());
                painter->rotate(-90);
                painter->translate(-r.left(), -r.top());
            }

            if (!dwOpt->title.isEmpty()) {
                QString titleText
                        = painter->fontMetrics().elidedText(dwOpt->title,
                                                            Qt::ElideRight, titleRect.width());
                proxy()->drawItemText(painter,
                                      titleRect,
                                      Qt::AlignLeft | Qt::AlignVCenter, dwOpt->palette,
                                      dwOpt->state & State_Enabled, titleText,
                                      QPalette::WindowText);
            }
            painter->setRenderHint(QPainter::Antialiasing, true);
            painter->setPen(opt->palette.color(QPalette::ButtonText).lighter(120));
            painter->setBrush(opt->palette.color(QPalette::Base).darker(120));
            painter->drawRoundedRect(QRectF(opt->rect).adjusted(0.5,0.5,-0.5,-0.5), 4, 4);
        }
        painter->restore();
        DEBUG_OUT_PUT("CE_DockWidgetTitle");
        break;
    case CE_ScrollBarAddLine:
    case CE_ScrollBarSubLine:
    {
        //if ((opt->state & State_Sunken))
        {
            painter->save();
            painter->setPen(Qt::NoPen);
            painter->setBrush(opt->palette.brush(QPalette::Window));
            painter->drawRect(opt->rect);
            painter->restore();
            if (!(opt->state & State_MouseOver))
            {
                break;
            }
        } /*else {
            QStyleOption buttonOpt = *opt;
            if (!(buttonOpt.state & State_Sunken))
                buttonOpt.state |= State_Raised;
            QPalette pal(opt->palette);
            pal.setColor(QPalette::Button, opt->palette.light().color());
            pal.setColor(QPalette::Light, opt->palette.button().color());
            qDrawWinButton(p, opt->rect, pal, opt->state & (State_Sunken | State_On),
                           &opt->palette.brush(QPalette::Button));
        }*/
        PrimitiveElement arrow;
        if (opt->state & State_Horizontal) {
            if (element == CE_ScrollBarAddLine)
                arrow = opt->direction == Qt::LeftToRight ? PE_IndicatorArrowRight : PE_IndicatorArrowLeft;
            else
                arrow = opt->direction == Qt::LeftToRight ? PE_IndicatorArrowLeft : PE_IndicatorArrowRight;
        } else {
            if (element == CE_ScrollBarAddLine)
                arrow = PE_IndicatorArrowDown;
            else
                arrow = PE_IndicatorArrowUp;
        }
        QStyleOption arrowOpt = *opt;
        arrowOpt.rect = opt->rect.adjusted(4, 4, -4, -4);
        proxy()->drawPrimitive(arrow, &arrowOpt, painter, widget);
        break;
    }
    case CE_ScrollBarAddPage:
    case CE_ScrollBarSubPage:
    {
        painter->save();
        QBrush br;
        QBrush bg = painter->background();
        Qt::BGMode bg_mode = painter->backgroundMode();
        painter->setPen(Qt::NoPen);
        painter->setBackgroundMode(Qt::OpaqueMode);

        if (opt->state & State_Sunken) {
            // br = QBrush(opt->palette.shadow().color(), Qt::Dense4Pattern);
            // p->setBackground(opt->palette.dark().color());
            // p->setBrush(br);
        } else {
            const QBrush paletteBrush = opt->palette.brush(QPalette::Light);
            if (paletteBrush.style() == Qt::TexturePattern) {
                if (qHasPixmapTexture(paletteBrush))
                    br = QBrush(paletteBrush.texture());
                else
                    br = QBrush(paletteBrush.textureImage());
            } else
                br = QBrush(opt->palette.light().color(), Qt::Dense4Pattern);
            painter->setBackground(opt->palette.window().color());
            painter->setBrush(br);
        }
        painter->drawRect(opt->rect);
        painter->setBackground(bg);
        painter->setBackgroundMode(bg_mode);
        painter->restore();
        break;
    }
    case CE_ScrollBarSlider:
    {
        const QStyleOptionSlider *optSlider = qstyleoption_cast<const QStyleOptionSlider *>(opt);
        if (optSlider)
        {
            painter->save();
            /*if (!(optSlider->state & State_Enabled)) {
                QBrush br;
                const QBrush paletteBrush = opt->palette.brush(QPalette::Light);
                if (paletteBrush.style() == Qt::TexturePattern) {
                    if (qHasPixmapTexture(paletteBrush))
                        br = QBrush(paletteBrush.texture());
                    else
                        br = QBrush(paletteBrush.textureImage());
                } else
                    br = QBrush(opt->palette.light().color(), Qt::Dense4Pattern);
            p->setPen(Qt::NoPen);
            p->setBrush(br);
            p->setBackgroundMode(Qt::OpaqueMode);
            p->drawRect(opt->rect);
            } else */{
                //sliderRect.setWidth(opt->rect.width()*0.1);
                QRectF sliderRect;
//                QLineF sliderLine;
//                sliderLine.setP1(QPointF(optSlider->rect.center().x(), optSlider->rect.center().y() - optSlider->rect.height() * 0.5));
//                sliderLine.setP2(QPointF(opt->rect.center().x(), opt->rect.bottom()));
                double Radius = 0;
                if (QStyle::State_Horizontal & optSlider->state)
                {
                    int nHeight = optSlider->rect.height()*0.2;
                    if (optSlider->state & State_MouseOver)
                    {
                        nHeight = optSlider->rect.height()*0.4;
                    }
//                    sliderLine.setP1(QPointF(optSlider->rect.center().x() - optSlider->rect.height() * 0.5, optSlider->rect.center().y()));
//                    sliderLine.setP2(QPointF(opt->rect.right(), opt->rect.center().y()));
                    sliderRect.setTop(optSlider->rect.center().y() - nHeight * 0.5);
                    sliderRect.setLeft(optSlider->rect.left());
                    sliderRect.setWidth(optSlider->rect.width());
                    sliderRect.setHeight(nHeight);
                    Radius = nHeight * 0.5;
                }
                else
                {
                    int nWidth = optSlider->rect.width()*0.2;
                    if (optSlider->state & State_MouseOver)
                    {
                        nWidth = optSlider->rect.width()*0.4;
                    }
                    sliderRect.setTop(optSlider->rect.top());
                    sliderRect.setLeft(optSlider->rect.center().x() - nWidth * 0.5);
                    sliderRect.setWidth(nWidth);
                    sliderRect.setHeight(optSlider->rect.height());
                    Radius = nWidth * 0.5;
                }
//                QPen pen = p->pen();
//                pen.setColor((optSlider->palette.color(QPalette::WindowText)));
//                pen.setWidth(nWidth);
//                pen.setCapStyle(Qt::RoundCap);
                painter->setPen(Qt::NoPen);
//                p->setBrush(Qt::NoBrush);
                if (optSlider->state & State_MouseOver)
                {
                    painter->setBrush(optSlider->palette.color(QPalette::WindowText));
                }
                else
                {
                    QColor normalColor = optSlider->palette.color(QPalette::WindowText);
                    normalColor.setAlpha(100);
                    painter->setBrush(normalColor);
                }
                if (!(optSlider->state & State_Enabled))
                {
                    painter->setBrush(optSlider->palette.color(QPalette::Disabled, QPalette::WindowText));
                }
                painter->setRenderHint(QPainter::Antialiasing);
                painter->drawRoundedRect(sliderRect, Radius, Radius);
                //p->drawLine(sliderLine);
                //qDrawWinButton(p, opt->rect, pal, false, &opt->palette.brush(QPalette::Button));
            }
            painter->restore();
        }
    }
        break;
    case CE_ScrollBarFirst:
    {
        break;
        //绘制滚动条的第一个按钮
        painter->save();
        painter->setPen(Qt::NoPen);
        painter->setBrush(Qt::red);
        painter->drawRect(opt->rect);
        painter->restore();
        int nWidth = opt->rect.width()*0.1;
        if (opt->state & State_MouseOver)
        {
            nWidth = opt->rect.width()*0.2;
        }
        QPen pen = painter->pen();
        pen.setColor(QColor(opt->palette.color(QPalette::Highlight)));
        pen.setWidth(nWidth);
        pen.setCapStyle(Qt::RoundCap);
        painter->setPen(pen);
        painter->drawLine(QPointF(opt->rect.center().x(), 0), QPointF(opt->rect.center().x(), opt->rect.bottom()));
    }
        break;
    case CE_ScrollBarLast:
    {
        break;
        painter->save();
        painter->setPen(Qt::NoPen);
        painter->setBrush(Qt::red);
        painter->drawRect(opt->rect);
        int nWidth = opt->rect.width()*0.1;
        if (opt->state & State_MouseOver)
        {
            nWidth = opt->rect.width()*0.2;
        }
        QPen pen = painter->pen();
        pen.setColor(QColor(opt->palette.color(QPalette::Highlight)));
        pen.setWidth(nWidth);
        pen.setCapStyle(Qt::RoundCap);
        painter->setPen(pen);
        painter->drawLine(QPointF(opt->rect.center().x(), 0), QPointF(opt->rect.center().x(), opt->rect.bottom()));
        painter->restore();
    }
        break;
    case CE_FocusFrame:
        DEBUG_OUT_PUT("CE_FocusFrame");
        break;
    case CE_ComboBoxLabel:
        if (const QStyleOptionComboBox *cb = qstyleoption_cast<const QStyleOptionComboBox*>(opt)) {
            // 绘制组合框文本
            QRect textRect = subControlRect(CC_ComboBox, cb, SC_ComboBoxEditField, widget);
            //textRect.adjust(12, 0, -40, 0); // 增加内边距

            QString text = cb->currentText;

            painter->save();
            painter->setPen(QColor(0x20, 0x20, 0x20));
            //painter->setFont(cb->font);
            painter->drawText(textRect, Qt::AlignLeft | Qt::AlignVCenter, text);
            uint alignment = visualAlignment(cb->direction, Qt::AlignLeft | Qt::AlignVCenter);
            proxy()->drawItemText(painter, textRect, alignment | Qt::TextShowMnemonic,
                                  cb->palette, cb->state & State_Enabled, cb->currentText, QPalette::WindowText);
            painter->restore();
            break;
        }
    case CE_ToolBar:
        DEBUG_OUT_PUT("CE_ToolBar");
        break;
    case CE_ToolBoxTabShape:
        DEBUG_OUT_PUT("CE_ToolBoxTabShape");
        break;
    case CE_ToolBoxTabLabel:
        DEBUG_OUT_PUT("CE_ToolBoxTabLabel");
        break;
    case CE_HeaderEmptyArea:
        DEBUG_OUT_PUT("CE_HeaderEmptyArea");
        break;
    case CE_ColumnViewGrip:
        DEBUG_OUT_PUT("CE_ColumnViewGrip");
        break;
    case CE_ItemViewItem:
        if (const QStyleOptionViewItem *vopt = qstyleoption_cast<const QStyleOptionViewItem *>(opt))
        {
            painter->save();
            painter->setClipRect(opt->rect);

            QRect checkRect = proxy()->subElementRect(SE_ItemViewItemCheckIndicator, vopt, widget);
            QRect iconRect = proxy()->subElementRect(SE_ItemViewItemDecoration, vopt, widget);
            QRect textRect = proxy()->subElementRect(SE_ItemViewItemText, vopt, widget);

            // draw the background
            proxy()->drawPrimitive(PE_PanelItemViewItem, opt, painter, widget);

            // draw the check mark
            if (vopt->features & QStyleOptionViewItem::HasCheckIndicator) {
                QStyleOptionViewItem option(*vopt);
                option.rect = checkRect;
                option.state = option.state & ~QStyle::State_HasFocus;

                switch (vopt->checkState) {
                case Qt::Unchecked:
                    option.state |= QStyle::State_Off;
                    break;
                case Qt::PartiallyChecked:
                    option.state |= QStyle::State_NoChange;
                    break;
                case Qt::Checked:
                    option.state |= QStyle::State_On;
                    break;
                }
                proxy()->drawPrimitive(QStyle::PE_IndicatorItemViewItemCheck, &option, painter, widget);
            }

            // draw the icon
            QIcon::Mode mode = QIcon::Normal;
            if (!(vopt->state & QStyle::State_Enabled))
                mode = QIcon::Disabled;
            else if (vopt->state & QStyle::State_Selected)
                mode = QIcon::Selected;
            QIcon::State state = vopt->state & QStyle::State_Open ? QIcon::On : QIcon::Off;
            vopt->icon.paint(painter, iconRect, vopt->decorationAlignment, mode, state);

            // draw the text
            if (!vopt->text.isEmpty()) {
                QPalette::ColorGroup cg = vopt->state & QStyle::State_Enabled
                                      ? QPalette::Normal : QPalette::Disabled;
                if (cg == QPalette::Normal && !(vopt->state & QStyle::State_Active))
                    cg = QPalette::Inactive;

                if (vopt->state & QStyle::State_Selected) {
                    painter->setPen(vopt->palette.color(cg, QPalette::HighlightedText));
                } else {
                    painter->setPen(vopt->palette.color(cg, QPalette::Text));
                }
                if (vopt->state & QStyle::State_Editing) {
                    painter->setPen(vopt->palette.color(cg, QPalette::Text));
                    painter->drawRect(textRect.adjusted(0, 0, -1, -1));
                }

                //d->viewItemDrawText(p, vopt, textRect);
            }

            // draw the focus rect
             if (vopt->state & QStyle::State_HasFocus) {
                QStyleOptionFocusRect o;
                o.QStyleOption::operator=(*vopt);
                o.rect = proxy()->subElementRect(SE_ItemViewItemFocusRect, vopt, widget);
                o.state |= QStyle::State_KeyboardFocusChange;
                o.state |= QStyle::State_Item;
                QPalette::ColorGroup cg = (vopt->state & QStyle::State_Enabled)
                              ? QPalette::Normal : QPalette::Disabled;
                o.backgroundColor = vopt->palette.color(cg, (vopt->state & QStyle::State_Selected)
                                             ? QPalette::Highlight : QPalette::Window);
                proxy()->drawPrimitive(QStyle::PE_FrameFocusRect, &o, painter, widget);
            }

             painter->restore();
        }
        break;
    case CE_ShapedFrame:
        if (const QStyleOptionFrame *f = qstyleoption_cast<const QStyleOptionFrame *>(opt)) {
            int frameShape  = f->frameShape;
            int frameShadow = QFrame::Plain;
            if (f->state & QStyle::State_Sunken) {
                frameShadow = QFrame::Sunken;
            } else if (f->state & QStyle::State_Raised) {
                frameShadow = QFrame::Raised;
            }

            int lw = f->lineWidth;
            int mlw = f->midLineWidth;
            QPalette::ColorRole foregroundRole = QPalette::WindowText;
            if (widget)
                foregroundRole = widget->foregroundRole();

            switch (frameShape) {
            case QFrame::Box:
                if (frameShadow == QFrame::Plain) {
                    qDrawPlainRect(painter, f->rect, f->palette.color(foregroundRole), lw);
                } else {
                    qDrawShadeRect(painter, f->rect, f->palette, frameShadow == QFrame::Sunken, lw, mlw);
                }
                break;
            case QFrame::StyledPanel:
                //keep the compatibility with Qt 4.4 if there is a proxy style.
                //be sure to call drawPrimitive(QStyle::PE_Frame) on the proxy style
                if (widget) {
                    widget->style()->drawPrimitive(QStyle::PE_Frame, opt, painter, widget);
                } else {
                    proxy()->drawPrimitive(QStyle::PE_Frame, opt, painter, widget);
                }
                break;
            case QFrame::Panel:
                if (frameShadow == QFrame::Plain) {
                    qDrawPlainRect(painter, f->rect, f->palette.color(foregroundRole), lw);
                } else {
                    qDrawShadePanel(painter, f->rect, f->palette, frameShadow == QFrame::Sunken, lw);
                }
                break;
            case QFrame::WinPanel:
                if (frameShadow == QFrame::Plain) {
                    qDrawPlainRect(painter, f->rect, f->palette.color(foregroundRole), lw);
                } else {
                    qDrawWinPanel(painter, f->rect, f->palette, frameShadow == QFrame::Sunken);
                }
                break;
            case QFrame::HLine:
            case QFrame::VLine: {
                QPoint p1, p2;
                if (frameShape == QFrame::HLine) {
                    p1 = QPoint(opt->rect.x(), opt->rect.y() + opt->rect.height() / 2);
                    p2 = QPoint(opt->rect.x() + opt->rect.width(), p1.y());
                } else {
                    p1 = QPoint(opt->rect.x() + opt->rect.width() / 2, opt->rect.y());
                    p2 = QPoint(p1.x(), p1.y() + opt->rect.height());
                }
                if (frameShadow == QFrame::Plain) {
                    QPen oldPen = painter->pen();
                    painter->setPen(QPen(opt->palette.brush(foregroundRole), lw));
                    painter->drawLine(p1, p2);
                    painter->setPen(oldPen);
                } else {
                    qDrawShadeLine(painter, p1, p2, f->palette, frameShadow == QFrame::Sunken, lw, mlw);
                }
                break;
            }
            }
        }
        break;
    default:
        break;
    }
#endif //#if
}

#if 0 //disable subElementRect
QRect CFluentStyle::subElementRect(QStyle::SubElement r, const QStyleOption *opt, const QWidget *widget) const
{

#if 1
    switch (r) {
    case SE_PushButtonContents:
        qDebug() << "SE_PushButtonContents";
        break;
    case SE_PushButtonFocusRect:
        qDebug() << "SE_PushButtonFocusRect";
        break;
    case SE_CheckBoxIndicator:
        qDebug() << "SE_CheckBoxIndicator";
        break;
    case SE_CheckBoxContents:
        qDebug() << "SE_CheckBoxContents";
        break;
    case SE_CheckBoxFocusRect:
        qDebug() << "SE_CheckBoxFocusRect";
        break;
    case SE_CheckBoxClickRect:
        qDebug() << "SE_CheckBoxClickRect";
        break;
    case SE_RadioButtonIndicator:
        qDebug() << "SE_RadioButtonIndicator";
        break;
    case SE_RadioButtonContents:
        qDebug() << "SE_RadioButtonContents";
        break;
    case SE_RadioButtonFocusRect:
        qDebug() << "SE_RadioButtonFocusRect";
        break;
    case SE_RadioButtonClickRect:
        qDebug() << "SE_RadioButtonClickRect";
        break;
    case SE_ComboBoxFocusRect:
        qDebug() << "SE_ComboBoxFocusRect";
        break;
    case SE_SliderFocusRect:
        qDebug() << "SE_SliderFocusRect";
        break;
    case SE_ProgressBarGroove:
        qDebug() << "SE_ProgressBarGroove";
        break;
    case SE_ProgressBarContents:
        qDebug() << "SE_ProgressBarContents";
        break;
    case SE_ProgressBarLabel:
        qDebug() << "SE_ProgressBarLabel";
        break;
    case SE_ToolBoxTabContents:
        qDebug() << "SE_ToolBoxTabContents";
        break;
    case SE_HeaderLabel:
        qDebug() << "SE_HeaderLabel";
        break;
    case SE_HeaderArrow:
        qDebug() << "SE_HeaderArrow";
        break;
    case SE_TabWidgetTabBar:
        qDebug() << "SE_TabWidgetTabBar";
        break;
    case SE_TabWidgetTabPane:
        qDebug() << "SE_TabWidgetTabPane";
        break;
    case SE_TabWidgetTabContents:
        qDebug() << "SE_TabWidgetTabContents";
        break;
    case SE_TabWidgetLeftCorner:
        qDebug() << "SE_TabWidgetLeftCorner";
        break;
    case SE_TabWidgetRightCorner:
        qDebug() << "SE_TabWidgetRightCorner";
        break;
    case SE_ItemViewItemCheckIndicator:
        qDebug() << "SE_ItemViewItemCheckIndicator";
        break;
    case SE_TabBarTearIndicator:
        qDebug() << "SE_TabBarTearIndicator";
        break;
    case SE_TreeViewDisclosureItem:
        qDebug() << "SE_TreeViewDisclosureItem";
        break;
    case SE_LineEditContents:
        qDebug() << "SE_LineEditContents";
        break;
    case SE_FrameContents:
        qDebug() << "SE_FrameContents";
        break;
    case SE_DockWidgetCloseButton:
        qDebug() << "SE_DockWidgetCloseButton";
        break;
    case SE_DockWidgetFloatButton:
        qDebug() << "SE_DockWidgetFloatButton";
        break;
    case SE_DockWidgetTitleBarText:
        qDebug() << "SE_DockWidgetTitleBarText";
        break;
    case SE_DockWidgetIcon:
        qDebug() << "SE_DockWidgetIcon";
        break;
    case SE_CheckBoxLayoutItem:
        qDebug() << "SE_CheckBoxLayoutItem";
        break;
    case SE_ComboBoxLayoutItem:
        qDebug() << "SE_ComboBoxLayoutItem";
        break;
    case SE_DateTimeEditLayoutItem:
        qDebug() << "SE_DateTimeEditLayoutItem";
        break;
    case SE_DialogButtonBoxLayoutItem:
        qDebug() << "SE_DialogButtonBoxLayoutItem";
        break;
    case SE_LabelLayoutItem:
        qDebug() << "SE_LabelLayoutItem";
        break;
    case SE_ProgressBarLayoutItem:
        qDebug() << "SE_ProgressBarLayoutItem";
        break;
    case SE_PushButtonLayoutItem:
        qDebug() << "SE_PushButtonLayoutItem";
        break;
    case SE_RadioButtonLayoutItem:
        qDebug() << "SE_RadioButtonLayoutItem";
        break;
    case SE_SliderLayoutItem:
        qDebug() << "SE_SliderLayoutItem";
        break;
    case SE_SpinBoxLayoutItem:
        qDebug() << "SE_SpinBoxLayoutItem";
        break;
    case SE_ToolButtonLayoutItem:
        qDebug() << "SE_ToolButtonLayoutItem";
        break;
    case SE_FrameLayoutItem:
        qDebug() << "SE_FrameLayoutItem";
        break;
    case SE_GroupBoxLayoutItem:
        qDebug() << "SE_GroupBoxLayoutItem";
        break;
    case SE_TabWidgetLayoutItem:
        qDebug() << "SE_TabWidgetLayoutItem";
        break;
    case SE_ItemViewItemDecoration:
        qDebug() << "SE_ItemViewItemDecoration";
        break;
    case SE_ItemViewItemText:
        qDebug() << "SE_ItemViewItemText";
        break;
    case SE_ItemViewItemFocusRect:
        qDebug() << "SE_ItemViewItemFocusRect";
        break;
    case SE_TabBarTabLeftButton:
        qDebug() << "SE_TabBarTabLeftButton";
        break;
    case SE_TabBarTabRightButton:
        qDebug() << "SE_TabBarTabRightButton";
        break;
    case SE_TabBarTabText:
        qDebug() << "SE_TabBarTabText";
        break;
    case SE_ShapedFrameContents:
        qDebug() << "SE_ShapedFrameContents";
        break;
    case SE_ToolBarHandle:
        qDebug() << "SE_ToolBarHandle";
        break;
    case SE_TabBarScrollLeftButton:
        qDebug() << "SE_TabBarScrollLeftButton";
        break;
    case SE_TabBarScrollRightButton:
        qDebug() << "SE_TabBarScrollRightButton";
        break;
    case SE_TabBarTearIndicatorRight:
        qDebug() << "SE_TabBarTearIndicatorRight";
        break;
    default:
        break;
    }
#endif //#if
    return QProxyStyle::subElementRect(r, opt, widget);
}
#endif //disable subElementRect end
void CFluentStyle::drawComplexControl(QStyle::ComplexControl cc, const QStyleOptionComplex *opt, QPainter *painter, const QWidget *widget) const
{
    //QCommonStyle::drawComplexControl(cc, opt, p, w);
#if 1

    switch (cc)
    {
    case CC_SpinBox:
        if (const QStyleOptionSpinBox *sb = qstyleoption_cast<const QStyleOptionSpinBox *>(opt)) {
            QStyleOptionSpinBox copy = *sb;
            PrimitiveElement pe;

            if (sb->frame && (sb->subControls & SC_SpinBoxFrame)) {
                QRect r = proxy()->subControlRect(CC_SpinBox, sb, SC_SpinBoxFrame, widget);
                painter->save();
                painter->setRenderHint(QPainter::Antialiasing);

                QColor frameColor;
                if (opt->state & QStyle::State_HasFocus)
                {
                    frameColor = opt->palette.highlight().color();
                }
                else
                {
                    frameColor = opt->palette.placeholderText().color();
                }
                QPen pen(frameColor, 1);
                painter->setPen(pen);
                painter->setBrush(opt->palette.button().color());

                painter->drawRoundedRect(r, 4, 4);

                painter->restore();
            }

            if (sb->subControls & SC_SpinBoxUp) {
                copy.subControls = SC_SpinBoxUp;
                QPalette pal2 = sb->palette;
                if (!(sb->stepEnabled & QAbstractSpinBox::StepUpEnabled)) {
                    pal2.setCurrentColorGroup(QPalette::Disabled);
                    copy.state &= ~State_Enabled;
                }

                copy.palette = pal2;

                if (sb->activeSubControls == SC_SpinBoxUp && (sb->state & State_Sunken)) {
                    copy.state |= State_On;
                    copy.state |= State_Sunken;
                } else {
                    copy.state |= State_Raised;
                    copy.state &= ~State_Sunken;
                }
                pe = (sb->buttonSymbols == QAbstractSpinBox::PlusMinus ? PE_IndicatorSpinPlus
                      : PE_IndicatorSpinUp);

                copy.rect = proxy()->subControlRect(CC_SpinBox, sb, SC_SpinBoxUp, widget);
                proxy()->drawPrimitive(PE_PanelButtonBevel, &copy, painter, widget);
                copy.rect.adjust(3, 0, -4, 0);
                proxy()->drawPrimitive(pe, &copy, painter, widget);
            }

            if (sb->subControls & SC_SpinBoxDown) {
                copy.subControls = SC_SpinBoxDown;
                copy.state = sb->state;
                QPalette pal2 = sb->palette;
                if (!(sb->stepEnabled & QAbstractSpinBox::StepDownEnabled)) {
                    pal2.setCurrentColorGroup(QPalette::Disabled);
                    copy.state &= ~State_Enabled;
                }
                copy.palette = pal2;

                if (sb->activeSubControls == SC_SpinBoxDown && (sb->state & State_Sunken)) {
                    copy.state |= State_On;
                    copy.state |= State_Sunken;
                } else {
                    copy.state |= State_Raised;
                    copy.state &= ~State_Sunken;
                }
                pe = (sb->buttonSymbols == QAbstractSpinBox::PlusMinus ? PE_IndicatorSpinMinus
                      : PE_IndicatorSpinDown);

                copy.rect = proxy()->subControlRect(CC_SpinBox, sb, SC_SpinBoxDown, widget);
                proxy()->drawPrimitive(PE_PanelButtonBevel, &copy, painter, widget);
                copy.rect.adjust(3, 0, -4, 0);
                proxy()->drawPrimitive(pe, &copy, painter, widget);
            }
        }
        break;
    case CC_ComboBox:
    {
        if (const QStyleOptionComboBox *cb = qstyleoption_cast<const QStyleOptionComboBox*>(opt)) {
            // 绘制组合框背景
            QRect rect = cb->rect;
            bool isEnabled = cb->state & State_Enabled;
            bool isHovered = cb->state & State_MouseOver;
            bool hasFocus = cb->state & State_HasFocus;
            bool isPressed = cb->state & State_Sunken;

            // 设置背景颜色
            QColor backgroundColor;
            if (!isEnabled) {
                backgroundColor = opt->palette.color(QPalette::Disabled, QPalette::Window);
            } else {
                backgroundColor = opt->palette.color(QPalette::Active, QPalette::Window);
            }

            // 设置边框颜色
            QColor borderColor;
            if (!isEnabled) {
                borderColor = opt->palette.color(QPalette::Disabled, QPalette::WindowText);
            } else if (hasFocus || isPressed) {
                borderColor = opt->palette.color(QPalette::Active, QPalette::Highlight); // 焦点状态 - 蓝色
            } else if (isHovered) {
                borderColor = opt->palette.color(QPalette::Active, QPalette::Highlight).lighter();
            } else {
                borderColor = opt->palette.color(QPalette::Active, QPalette::WindowText);
            }

            // 绘制背景
            painter->setRenderHint(QPainter::Antialiasing, true);
            painter->setPen(Qt::NoPen);
            painter->setBrush(backgroundColor);
            painter->drawRoundedRect(rect, 4, 4);

            // 绘制边框
            int frameLineWidth = isHovered || hasFocus || isPressed ? 2 : 1;
            QPen borderPen(borderColor, frameLineWidth);
            painter->setPen(borderPen);
            painter->setBrush(Qt::NoBrush);
            double adjustVal = double(frameLineWidth)/2.0;
            painter->drawRoundedRect(QRectF(rect).adjusted(adjustVal, adjustVal, -adjustVal, -adjustVal), 4, 4);

            // 绘制下拉按钮
            QRect buttonRect = subControlRect(CC_ComboBox, cb, SC_ComboBoxArrow, widget);
            buttonRect.adjust(0, 0, -8, 0); // 向右偏移

            // 按钮背景（悬停/按下状态）
//            if (isHovered || isPressed) {
//                QColor buttonBg = QColor(0xF0, 0xF0, 0xF0);
//                if (isPressed) {
//                    buttonBg = QColor(0xE5, 0xE5, 0xE5);
//                }
//                p->setPen(Qt::NoPen);
//                p->setBrush(buttonBg);
//                //p->drawRoundedRect(buttonRect.adjusted(0, 4, 0, -4), 4, 4);
//            }

            // 绘制下拉箭头
            QColor arrowColor = isEnabled ?
                                    opt->palette.color(QPalette::Active, QPalette::WindowText) :
                                    (opt->palette.color(QPalette::Disabled, QPalette::WindowText));
            if (isHovered || hasFocus)
            {
                arrowColor = opt->palette.color(QPalette::Active, QPalette::Highlight);
            }

            painter->setPen(QPen(arrowColor, 1.5, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
            painter->setBrush(Qt::NoBrush);

            // 创建箭头路径
            QPainterPath path;
            int arrowSize = 6;
            int centerX = buttonRect.center().x();
            int centerY = buttonRect.center().y() + 1;

            path.moveTo(centerX - arrowSize/2, centerY - arrowSize/3);
            path.lineTo(centerX, centerY + arrowSize/3);
            path.lineTo(centerX + arrowSize/2, centerY - arrowSize/3);
            painter->drawPath(path);

            return;
        }
    }
        break;
    case CC_ScrollBar:
        if (const QStyleOptionSlider *scrollbar = qstyleoption_cast<const QStyleOptionSlider *>(opt)) {
            // Make a copy here and reset it for each primitive.
            QStyleOptionSlider newScrollbar = *scrollbar;
            State saveFlags = scrollbar->state;

            {
                newScrollbar.state = saveFlags;
                newScrollbar.rect = opt->rect;
                if (newScrollbar.rect.isValid()) {
                    if (!(scrollbar->activeSubControls & SC_ScrollBarSubLine))
                        newScrollbar.state &= ~(State_Sunken /*| State_MouseOver*/);
                    //proxy()->drawControl(CE_ScrollBarSubLine, &newScrollbar, p, w);
                    painter->save();
                    painter->setPen(Qt::NoPen);
                    painter->setBrush(opt->palette.brush(QPalette::Window));
                    painter->drawRect(opt->rect);
                    painter->restore();
                }
            }

            if (scrollbar->subControls & SC_ScrollBarSubLine) {
                newScrollbar.state = saveFlags;
                newScrollbar.rect = proxy()->subControlRect(cc, &newScrollbar, SC_ScrollBarSubLine, widget);
                if (newScrollbar.rect.isValid()) {
                    if (!(scrollbar->activeSubControls & SC_ScrollBarSubLine))
                        newScrollbar.state &= ~(State_Sunken /*| State_MouseOver*/);
                    proxy()->drawControl(CE_ScrollBarSubLine, &newScrollbar, painter, widget);
                }
            }
            if (scrollbar->subControls & SC_ScrollBarAddLine) {
                newScrollbar.rect = scrollbar->rect;
                newScrollbar.state = saveFlags;
                newScrollbar.rect = proxy()->subControlRect(cc, &newScrollbar, SC_ScrollBarAddLine, widget);
                if (newScrollbar.rect.isValid()) {
                    if (!(scrollbar->activeSubControls & SC_ScrollBarAddLine))
                        newScrollbar.state &= ~(State_Sunken /*| State_MouseOver*/);
                    proxy()->drawControl(CE_ScrollBarAddLine, &newScrollbar, painter, widget);
                }
            }
            if (scrollbar->subControls & SC_ScrollBarSubPage) {
                newScrollbar.rect = scrollbar->rect;
                newScrollbar.state = saveFlags;
                newScrollbar.rect = proxy()->subControlRect(cc, &newScrollbar, SC_ScrollBarSubPage, widget);
                if (newScrollbar.rect.isValid()) {
                    if (!(scrollbar->activeSubControls & SC_ScrollBarSubPage))
                        newScrollbar.state &= ~(State_Sunken /*| State_MouseOver*/);
                    proxy()->drawControl(CE_ScrollBarSubPage, &newScrollbar, painter, widget);
                }
            }
            if (scrollbar->subControls & SC_ScrollBarAddPage) {
                newScrollbar.rect = scrollbar->rect;
                newScrollbar.state = saveFlags;
                newScrollbar.rect = proxy()->subControlRect(cc, &newScrollbar, SC_ScrollBarAddPage, widget);
                if (newScrollbar.rect.isValid()) {
                    // if (!(scrollbar->activeSubControls & SC_ScrollBarAddPage))
                    //     newScrollbar.state &= ~(State_Sunken | State_MouseOver);
                    proxy()->drawControl(CE_ScrollBarAddPage, &newScrollbar, painter, widget);
                }
            }
            // if (scrollbar->subControls & SC_ScrollBarFirst) {
            //     newScrollbar.rect = scrollbar->rect;
            //     newScrollbar.state = saveFlags;
            //     newScrollbar.rect = proxy()->subControlRect(cc, &newScrollbar, SC_ScrollBarFirst, w);
            //     if (newScrollbar.rect.isValid()) {
            //         if (!(scrollbar->activeSubControls & SC_ScrollBarFirst))
            //             newScrollbar.state &= ~(State_Sunken | State_MouseOver);
            //         proxy()->drawControl(CE_ScrollBarFirst, &newScrollbar, p, w);
            //     }
            // }
            // if (scrollbar->subControls & SC_ScrollBarLast) {
            //     newScrollbar.rect = scrollbar->rect;
            //     newScrollbar.state = saveFlags;
            //     newScrollbar.rect = proxy()->subControlRect(cc, &newScrollbar, SC_ScrollBarLast, w);
            //     if (newScrollbar.rect.isValid()) {
            //         if (!(scrollbar->activeSubControls & SC_ScrollBarLast))
            //             newScrollbar.state &= ~(State_Sunken | State_MouseOver);
            //         proxy()->drawControl(CE_ScrollBarLast, &newScrollbar, p, w);
            //     }
            // }
            if (scrollbar->subControls & SC_ScrollBarSlider) {
                newScrollbar.rect = scrollbar->rect;
                newScrollbar.state = saveFlags;
                newScrollbar.rect = proxy()->subControlRect(cc, &newScrollbar, SC_ScrollBarSlider, widget);
                if (newScrollbar.rect.isValid()) {
                    if (!(scrollbar->activeSubControls & SC_ScrollBarSlider))
                        newScrollbar.state &= ~(State_Sunken/* | State_MouseOver*/);
                    proxy()->drawControl(CE_ScrollBarSlider, &newScrollbar, painter, widget);

//                    if (scrollbar->state & State_HasFocus) {
//                        QStyleOptionFocusRect fropt;
//                        fropt.QStyleOption::operator=(newScrollbar);
//                        fropt.rect.setRect(newScrollbar.rect.x() + 2, newScrollbar.rect.y() + 2,
//                                           newScrollbar.rect.width() - 5,
//                                           newScrollbar.rect.height() - 5);
//                        proxy()->drawPrimitive(PE_FrameFocusRect, &fropt, p, w);
//                    }
                }
            }
        }
        break;
    case CC_Slider:
        if (const QStyleOptionSlider *slider = qstyleoption_cast<const QStyleOptionSlider *>(opt)) {
            QRect groove = proxy()->subControlRect(CC_Slider, opt, SC_SliderGroove, widget);
            QRect handle = proxy()->subControlRect(CC_Slider, opt, SC_SliderHandle, widget);

            bool horizontal = slider->orientation == Qt::Horizontal;
//            bool ticksAbove = slider->tickPosition & QSlider::TicksAbove;
//            bool ticksBelow = slider->tickPosition & QSlider::TicksBelow;

            painter->save();
            painter->setRenderHint(QPainter::Antialiasing);
            int nHandleWidth = handle.width();
            int nHandleHeight = handle.height();
            //绘制槽
            int nHeight = 0;
            int nWidth = 0;
            const QRect tmp = groove;
            int nRadius = 0;
            if (!horizontal)
            {
                nWidth = 4;
                nWidth = (nWidth % 2) == 0 ? nWidth : nWidth + 1;
                nHeight = groove.height();

                groove.setTop(tmp.top());
                groove.setLeft(tmp.center().x() - (nWidth * 0.5));
                groove.setBottom(tmp.bottom());
                groove.setRight(tmp.center().x() + (nWidth * 0.5));
                nRadius = nWidth * 0.5;
            }
            else
            {
                nHeight = 4;
                nHeight = (nHeight % 2) == 0 ? nHeight : nHeight + 1;
                nWidth = groove.width();
                groove.setTop(tmp.center().y() - (nHeight * 0.5));
                groove.setLeft(tmp.left());
                groove.setBottom(tmp.center().y() + (nHeight * 0.5));
                groove.setRight(tmp.right());
                nRadius = nHeight * 0.5;
            }
            painter->setPen(Qt::NoPen);
            painter->setBrush(opt->palette.color(QPalette::Disabled, QPalette::ButtonText));
            painter->drawRoundedRect(groove, nRadius, nRadius);

            //绘制左侧或者下侧的槽
            double dbProportion = double(slider->sliderPosition - slider->minimum) / double(slider->maximum - slider->minimum);
            if (!horizontal)
            {
                groove.setTop(groove.top() + (groove.height() * (1 - dbProportion)));
            }
            else
            {
                groove.setRight(groove.right() * dbProportion);
            }

            painter->setBrush(opt->palette.color(QPalette::Active, QPalette::Highlight));
            painter->drawRoundedRect(groove, nRadius, nRadius);

            //绘制handle
            double radius = (nHandleWidth > nHandleHeight ? nHandleHeight : nHandleWidth) * 0.5;

            painter->setPen(opt->palette.placeholderText().color());
            painter->setBrush(opt->palette.button().color());
            painter->drawEllipse(QRectF(handle).center(), radius, radius);

            painter->setPen(Qt::NoPen);
            painter->setBrush(opt->palette.highlight().color());
            if (opt->state & QStyle::State_Sunken)
            {
                radius *= 0.4;
            }
            else
            {
                if (opt->state & QStyle::State_MouseOver)
                {
                    radius *= 0.8;
                }
                else
                {
                    radius *= 0.6;
                }
            }
            painter->drawEllipse(QRectF(handle).center(), radius, radius);

            painter->restore();
        }
        break;
    case CC_ToolButton:
        if (const QStyleOptionToolButton *toolbutton
            = qstyleoption_cast<const QStyleOptionToolButton *>(opt)) {
            QRect button, menuarea;
            button = proxy()->subControlRect(cc, toolbutton, SC_ToolButton, widget);
            menuarea = proxy()->subControlRect(cc, toolbutton, SC_ToolButtonMenu, widget);

            State bflags = toolbutton->state & ~State_Sunken;

            if (bflags & State_AutoRaise) {
                if (!(bflags & State_MouseOver) || !(bflags & State_Enabled)) {
                    bflags &= ~State_Raised;
                }
            }
            State mflags = bflags;
            if (toolbutton->state & State_Sunken) {
                if (toolbutton->activeSubControls & SC_ToolButton)
                    bflags |= State_Sunken;
                mflags |= State_Sunken;
            }

            QStyleOption tool = *toolbutton;
            if (toolbutton->subControls & SC_ToolButton) {
                if (bflags & (State_Sunken | State_On | State_Raised)) {
                    tool.rect = button;
                    tool.state = bflags;
                    if (toolbutton->features & QStyleOptionToolButton::HasMenu) {
                        int mbi = proxy()->pixelMetric(PM_MenuButtonIndicator, toolbutton, widget);
                        QRect ir = toolbutton->rect;
                        QRect menuRect;
                        menuRect = QRect(ir.right() + 5 - mbi, ir.y() + ir.height() - mbi + 4, mbi - 6, mbi - 6);
                        menuRect = visualRect(toolbutton->direction, button, menuRect);
                        tool.rect |= menuRect;
                    }
                    proxy()->drawPrimitive(PE_PanelButtonTool, &tool, painter, widget);
                }
            }

            if (toolbutton->state & State_HasFocus) {
                QStyleOptionFocusRect fr;
                fr.QStyleOption::operator=(*toolbutton);
                fr.rect.adjust(3, 3, -3, -3);
                if (toolbutton->features & QStyleOptionToolButton::MenuButtonPopup)
                    fr.rect.adjust(0, 0, -proxy()->pixelMetric(QStyle::PM_MenuButtonIndicator,
                                                      toolbutton, widget), 0);
                proxy()->drawPrimitive(PE_FrameFocusRect, &fr, painter, widget);
            }
            QStyleOptionToolButton label = *toolbutton;
            label.state = bflags;
            int fw = proxy()->pixelMetric(PM_DefaultFrameWidth, opt, widget);
            label.rect = button.adjusted(fw, fw, -fw, -fw);
            proxy()->drawControl(CE_ToolButtonLabel, &label, painter, widget);

            if (toolbutton->subControls & SC_ToolButtonMenu) {
                tool.rect = menuarea;
                tool.state = mflags;
                if (mflags & (State_Sunken | State_On | State_Raised))
                    proxy()->drawPrimitive(PE_IndicatorButtonDropDown, &tool, painter, widget);
                proxy()->drawPrimitive(PE_IndicatorArrowDown, &tool, painter, widget);
            } else if (toolbutton->features & QStyleOptionToolButton::HasMenu) {
                int mbi = proxy()->pixelMetric(PM_MenuButtonIndicator, toolbutton, widget);
                QRect ir = toolbutton->rect;
                QStyleOptionToolButton newBtn = *toolbutton;
                newBtn.rect = QRect(ir.right() + 5 - mbi, ir.y() + ir.height() - mbi + 4, mbi - 6, mbi - 6);
                newBtn.rect = visualRect(toolbutton->direction, button, newBtn.rect);
                proxy()->drawPrimitive(PE_IndicatorArrowDown, &newBtn, painter, widget);
            }
        }
        break;
    case CC_TitleBar:
        DEBUG_OUT_PUT("CC_SpinBox");
        break;
    case CC_Dial:
        DEBUG_OUT_PUT("CC_SpinBox");
        break;
    case CC_GroupBox:
        if (const QStyleOptionGroupBox *groupBox = qstyleoption_cast<const QStyleOptionGroupBox *>(opt)) {
            // Draw frame
            QRect textRect = proxy()->subControlRect(CC_GroupBox, opt, SC_GroupBoxLabel, widget);
            QRect checkBoxRect = proxy()->subControlRect(CC_GroupBox, opt, SC_GroupBoxCheckBox, widget);
            if (groupBox->subControls & QStyle::SC_GroupBoxFrame) {
                QStyleOptionFrame frame;
                frame.QStyleOption::operator=(*groupBox);
                frame.features = groupBox->features;
                frame.lineWidth = groupBox->lineWidth;
                frame.midLineWidth = groupBox->midLineWidth;
                frame.rect = proxy()->subControlRect(CC_GroupBox, opt, SC_GroupBoxFrame, widget);
                painter->save();
                QRegion region(groupBox->rect);
                if (!groupBox->text.isEmpty()) {
                    bool ltr = groupBox->direction == Qt::LeftToRight;
                    QRect finalRect;
                    if (groupBox->subControls & QStyle::SC_GroupBoxCheckBox) {
                        finalRect = checkBoxRect.united(textRect);
                        finalRect.adjust(ltr ? -4 : 0, 0, ltr ? 0 : 4, 0);
                    } else {
                        finalRect = textRect;
                    }
                    region -= finalRect;
                }
                painter->setClipRegion(region);
                proxy()->drawPrimitive(PE_FrameGroupBox, &frame, painter, widget);
                painter->restore();
            }

            // Draw title
            if ((groupBox->subControls & QStyle::SC_GroupBoxLabel) && !groupBox->text.isEmpty()) {
                QColor textColor = groupBox->textColor;
                if (textColor.isValid())
                    painter->setPen(textColor);
                int alignment = int(groupBox->textAlignment);
                if (!proxy()->styleHint(QStyle::SH_UnderlineShortcut, opt, widget))
                    alignment |= Qt::TextHideMnemonic;

                proxy()->drawItemText(painter, textRect,  Qt::TextShowMnemonic | Qt::AlignHCenter | alignment,
                             groupBox->palette, groupBox->state & State_Enabled, groupBox->text,
                             textColor.isValid() ? QPalette::NoRole : QPalette::WindowText);

//                if (groupBox->state & State_HasFocus) {
//                    QStyleOptionFocusRect fropt;
//                    fropt.QStyleOption::operator=(*groupBox);
//                    fropt.rect = textRect;
//                    proxy()->drawPrimitive(PE_FrameFocusRect, &fropt, p, w);
//                }
            }

            // Draw checkbox
            if (groupBox->subControls & SC_GroupBoxCheckBox) {
                QStyleOptionButton box;
                box.QStyleOption::operator=(*groupBox);
                box.rect = checkBoxRect;
                proxy()->drawPrimitive(PE_IndicatorCheckBox, &box, painter, widget);
            }
        }
        break;
    case CC_MdiControls:
        DEBUG_OUT_PUT("CC_SpinBox");
        break;
    default:
        break;
    }

#endif //#if
}
#if 0
QRect CFluentStyle::subControlRect(QStyle::ComplexControl cc, const QStyleOptionComplex *opt, QStyle::SubControl sc, const QWidget *widget) const
{
    QRect ret;
    switch (cc) {
    case CC_Slider:
        if (const QStyleOptionSlider *slider = qstyleoption_cast<const QStyleOptionSlider *>(opt)) {
            int tickOffset = proxy()->pixelMetric(PM_SliderTickmarkOffset, slider, widget);
            int thickness = proxy()->pixelMetric(PM_SliderControlThickness, slider, widget);

            switch (sc) {
            case SC_SliderHandle: {
                int sliderPos = 0;
                int len = proxy()->pixelMetric(PM_SliderLength, slider, widget);
                bool horizontal = slider->orientation == Qt::Horizontal;
                sliderPos = sliderPositionFromValue(slider->minimum, slider->maximum,
                                                    slider->sliderPosition,
                                                    (horizontal ? slider->rect.width()
                                                                : slider->rect.height()) - len,
                                                    slider->upsideDown);
                if (horizontal)
                    ret.setRect(slider->rect.x() + sliderPos, slider->rect.y() + tickOffset, thickness, thickness);
                else
                    ret.setRect(slider->rect.x() + tickOffset, slider->rect.y() + sliderPos, thickness, len);

                break; }
            case SC_SliderGroove:
                if (slider->orientation == Qt::Horizontal)
                    ret.setRect(slider->rect.x(), slider->rect.y() + tickOffset,
                                slider->rect.width(), thickness);
                else
                    ret.setRect(slider->rect.x() + tickOffset, slider->rect.y(),
                                thickness, slider->rect.height());
                break;
            default:
                break;
            }
            ret = visualRect(slider->direction, slider->rect, ret);
        }
        break;
    default:
        ret = QProxyStyle::subControlRect(cc, opt, sc, widget);
        break;
    }

    return ret;
}
#endif
void CFluentStyle::DrawDefaultButton(const QStyleOption *opt, QPainter *p) const
{
    QBrush brush = opt->palette.brush(QPalette::Button);
    p->save();
    p->setRenderHint(QPainter::Antialiasing);

    QColor fillBgColor(opt->palette.color(QPalette::Active, QPalette::Highlight));

    if (opt->state & QStyle::State_MouseOver)
    {
        fillBgColor = fillBgColor.lighter(110);
    }
    // 设置画笔
    p->setPen(Qt::NoPen);
    p->setBrush(opt->palette.color(QPalette::Active, QPalette::Highlight).darker(120));
    // 绘制底色
    p->drawRoundedRect(opt->rect/*.adjusted(2, 2, -2, -2)*/, 6, 6);

    //绘制填充色
    p->setBrush(fillBgColor);
    p->drawRoundedRect(opt->rect.adjusted(2, 2, -2, -2), 6, 6);

    p->restore();
}

void CFluentStyle::DrawStandardButton(const QStyleOption *opt, QPainter *p, const QWidget *w) const
{
    const QStyleOptionButton *btn = qstyleoption_cast<const QStyleOptionButton*>(opt);
    if (!btn)
    {
        qWarning() << "CFluentStyle::DrawStandardButton: Invalid style option.";
        return;
    }
    p->save();
    p->setRenderHint(QPainter::Antialiasing);

    // QLinearGradient gradient(0, 0, opt->rect.width(), opt->rect.height());
    // gradient.setSpread(QLinearGradient::ReflectSpread);

    bool bDefault = false;
    if (w)
    {
        const QPushButton *pb = qobject_cast<const QPushButton*>(w);
        if (pb)
        {
            bDefault = pb->isDefault();
        }
    }
    if (bDefault)
    {
        return;
    }
    QRect rect = btn->rect;
    bool isEnabled = btn->state & State_Enabled;
    bool isHovered = btn->state & State_MouseOver;
    bool isPressed = btn->state & State_Sunken;
    bool isChecked = btn->state & State_On;
    bool isDefault = btn->features & QStyleOptionButton::DefaultButton;

    // 设置圆角半径
    const int borderRadius = 4;
    // 计算按钮状态颜色
    QColor backgroundColor;
    if (!isEnabled) {
        backgroundColor = opt->palette.color(QPalette::Disabled, QPalette::Button); // 禁用状态
    } else if (isPressed) {
        backgroundColor = opt->palette.color(QPalette::Disabled, QPalette::Highlight).lighter(200); // 按下状态
    } else if (isHovered) {
        backgroundColor = opt->palette.color(QPalette::Disabled, QPalette::Highlight).lighter(200); // 悬停状态
    } else {
        backgroundColor = opt->palette.color(QPalette::Active, QPalette::Button);; // 正常状态
    }
    QColor topLeftRightColor = QColor(0xd0, 0xd0, 0xd0);
    if (isHovered) topLeftRightColor = QColor(0x99, 0xc1, 0xe8);
    if (isPressed) topLeftRightColor = QColor(0x66, 0xa0, 0xdf);
    // 绘制按钮背景
    p->setPen(topLeftRightColor);
    p->setBrush(backgroundColor);
    p->drawRoundedRect(rect.adjusted(1,1,-1,-1), borderRadius, borderRadius);

    // 绘制特殊边框效果
    QPen borderPen;
    borderPen.setWidth(1);

    if (isEnabled) {
        // 顶部、左侧、右侧边框（浅色）

        // 底部边框（深色）
        QColor bottomColor = QColor(0x60, 0x60, 0x60);
        if (isHovered) bottomColor = QColor(0x3a, 0x6e, 0xa9);
        if (isPressed) bottomColor = QColor(0x00, 0x57, 0xb0);

        // // 绘制底部边框
        QPainterPath bottomPath;
        bottomPath.moveTo(rect.left() + borderRadius, rect.bottom());
        bottomPath.lineTo(rect.right() - borderRadius, rect.bottom());
        borderPen.setColor(bottomColor);
        p->setPen(borderPen);
        p->drawPath(bottomPath);

        // 绘制圆角部分的特殊效果
        //drawCornerBorders(rect, borderRadius, topLeftRightColor, bottomColor, p);
    }

    // 绘制焦点指示器
    if (isChecked) {
        QPen focusPen(QColor(0x00, 0x78, 0xd7));
        focusPen.setWidth(2);
        focusPen.setStyle(Qt::DotLine);
        p->setPen(Qt::NoPen);
        p->setBrush(opt->palette.color(QPalette::Disabled, QPalette::Highlight));
        p->drawRoundedRect(rect.adjusted(1, 1, -1, -1), borderRadius, borderRadius);
    }

    // 绘制默认按钮指示器
    if (isDefault && isEnabled) {
        QPen defaultPen(QColor(0x00, 0x78, 0xd7));
        defaultPen.setWidth(1);
        p->setPen(defaultPen);
        p->setBrush(Qt::NoBrush);
        p->drawRoundedRect(rect.adjusted(0, 0, -1, -1), borderRadius, borderRadius);
    }

    p->restore();
}

const QColor &CFluentStyle::StandardButtonLabelColor() const
{
    return d_ptr->m_standardButtonLabelColor;
}

const QColor &CFluentStyle::DefaultButtonLabelColor() const
{
    return d_ptr->m_defaultButtonLabelColor;
}

void CFluentStyle::DrawFluentCheckBox(const QStyleOption *option, QPainter *painter, const QWidget *widget) const
{
    Q_UNUSED(widget);
//    QStyleOptionButton button;
//    button.initFrom(widget);
//    button.rect = option->rect;
//    button.state = option->state;
//    button.features = option->features;

    // Draw checkbox outline
    painter->setPen(Qt::NoPen);
    QColor borderColor = QColor("#BFBFBF");
    if (option->state & State_MouseOver)
        borderColor = QColor("#979797");
    else if (option->state & State_On)
        borderColor = QColor("#0078D7");
    painter->setBrush(borderColor);
    painter->drawRoundedRect(option->rect.adjusted(0, 1, 0, 0), 2, 2);

    // Draw checkmark
    if (option->state & State_On) {
        painter->setPen(Qt::NoPen);
        painter->setBrush(QColor("#0078D7"));
        QPainterPath checkmark;
        checkmark.moveTo(option->rect.left() + option->rect.width() / 4, option->rect.top() + option->rect.height() / 2);
        checkmark.lineTo(option->rect.left() + option->rect.width() / 2, option->rect.top() + 3 * option->rect.height() / 4);
        checkmark.lineTo(option->rect.left() + 3 * option->rect.width() / 4, option->rect.top() + option->rect.height() / 4);
        painter->drawPath(checkmark);
    }
}

void CFluentStyle::tabLayout(const QStyleOptionTab *opt, const QWidget *widget, QRect *textRect, QRect *iconRect) const
{
    Q_ASSERT(textRect);
    Q_ASSERT(iconRect);
    QRect tr = opt->rect;
    bool verticalTabs = opt->shape == QTabBar::RoundedEast
                        || opt->shape == QTabBar::RoundedWest
                        || opt->shape == QTabBar::TriangularEast
                        || opt->shape == QTabBar::TriangularWest;
    if (verticalTabs)
        tr.setRect(0, 0, tr.height(), tr.width()); // 0, 0 as we will have a translate transform

    int verticalShift = proxy()->pixelMetric(QStyle::PM_TabBarTabShiftVertical, opt, widget);
    int horizontalShift = proxy()->pixelMetric(QStyle::PM_TabBarTabShiftHorizontal, opt, widget);
    int hpadding = proxy()->pixelMetric(QStyle::PM_TabBarTabHSpace, opt, widget) / 2;
    int vpadding = proxy()->pixelMetric(QStyle::PM_TabBarTabVSpace, opt, widget) / 2;
    if (opt->shape == QTabBar::RoundedSouth || opt->shape == QTabBar::TriangularSouth)
        verticalShift = -verticalShift;
    tr.adjust(hpadding, verticalShift - vpadding, horizontalShift - hpadding, vpadding);
    bool selected = opt->state & QStyle::State_Selected;
    if (selected) {
        tr.setTop(tr.top() - verticalShift);
        tr.setRight(tr.right() - horizontalShift);
    }

    // left widget
    if (!opt->leftButtonSize.isEmpty()) {
        tr.setLeft(tr.left() + 4 +
            (verticalTabs ? opt->leftButtonSize.height() : opt->leftButtonSize.width()));
    }
    // right widget
    if (!opt->rightButtonSize.isEmpty()) {
        tr.setRight(tr.right() - 4 -
            (verticalTabs ? opt->rightButtonSize.height() : opt->rightButtonSize.width()));
    }

    // icon
    if (!opt->icon.isNull()) {
        QSize iconSize = opt->iconSize;
        if (!iconSize.isValid()) {
            int iconExtent = proxy()->pixelMetric(QStyle::PM_SmallIconSize);
            iconSize = QSize(iconExtent, iconExtent);
        }
        QSize tabIconSize = opt->icon.actualSize(iconSize,
                        (opt->state & QStyle::State_Enabled) ? QIcon::Normal : QIcon::Disabled,
                        (opt->state & QStyle::State_Selected) ? QIcon::On : QIcon::Off);
        // High-dpi icons do not need adjustment; make sure tabIconSize is not larger than iconSize
        tabIconSize = QSize(qMin(tabIconSize.width(), iconSize.width()), qMin(tabIconSize.height(), iconSize.height()));

        const int offsetX = (iconSize.width() - tabIconSize.width()) / 2;
        *iconRect = QRect(tr.left() + offsetX, tr.center().y() - tabIconSize.height() / 2,
                          tabIconSize.width(), tabIconSize.height());
        if (!verticalTabs)
            *iconRect = QStyle::visualRect(opt->direction, opt->rect, *iconRect);
        tr.setLeft(tr.left() + tabIconSize.width() + 4);
    }

    if (!verticalTabs)
        tr = QStyle::visualRect(opt->direction, opt->rect, tr);

    *textRect = tr;
}

CMyTestStylePrivate::CMyTestStylePrivate()
    : m_defaultButtonColor(defaultButtonColor)
    , m_standardButtonLabelColor(standardButtonLabelColor)
    , m_defaultButtonLabelColor(defaultButtonLabelColor)
{

}
